O'Reilly logo

PHP for Teens by Maneesh Sethi

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Cookie Exercise

Before leaving this chapter, let’s go over the exercise regarding using cookies and a session in conjunction with each other to create a counter that always updates itself, even after the session has been ended and the browser has been closed. We are going to do this exercise by adding and updating a cookie every time the user visits the site. The following code answers this problem. Take a look and see if you can understand it all the way through without looking at the explanation.

<?php session_name("counter"); session_start(); header("Cache-control:private"); $_SESSION['counter'] = $_COOKIE['counter']; $_SESSION['counter']++; setcookie("counter", $_SESSION['counter'], time()+ 60 * 60 * 24 * 30); echo "Hey, welcome to this web ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required