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 page. While you are here, feel free to browse
  some other sites. Just make sure you come back!. Hey, you might as well check
out <a href="http://www.maneeshsethi.com"> ManeeshSethi.com </a> while you
  are here!";

echo "<p>We will keep a record of how many times you come back!";

echo "<p><p><div align="center">You have visited our page
{$_SESSION['counter']} times.";

?>

This page uses cookies and sessions in conjunction to create a counter that remembers how many times you have visited the site even after you close the page! The page begins by setting up the session and assigning a session variable to the cookie variable. If the cookie variable does not exist, the session variable is assigned to 0. You might be wondering how the variable knows how to set the cookie variable to 0. If the variable does not exist, it automatically has a value of 0. This line performs that feat:

$_SESSION['counter'] = $_COOKIE['counter'];

The next line increments the $_SESSION['counter'] variable, updating the counter. Use this value for all future calculations in the script; it has the most updated counter value.

Next is the main line: It sets the cookie that recalls the value every time we load the page. The cookie has several important parameters, so take a look at the line of code:

setcookie("counter", $_SESSION['counter'], time()+ 60 * 60 * 24 * 30);

The first parameter is the cookie name. We used it in one of the earlier assigning lines of code. The second parameter is the cookie value. Notice that was the counter’s $_SESSION value rather than the cookie value. This is because we updated the $_SESSION value very recently, and the $_COOKIE value has not been updated. If we assigned the cookie with the same value of $_COOKIE, the web page would never update itself. In other words, the cookie would remain at 1 forever.

The last parameter refers to how long the cookie will last. We set it for 60 * 60 * 24 * 30, which can also be read as simply 30 days. After that time, the cookie disappears and the counter resets. You can change the expire time.

The last few lines of code simply echo out the number of times the counter has been echoed. Check out what this page looks like in Figure 13.19.

Figure 13.19. The phpft13-06.php counter page.


 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.226.105