- Session Cookies: Cookies That Expire When the Browser is Closed
- Persistent Cookies: Cookies That Expire After X Seconds
- Deleting Cookies
- Finding More Information
Persistent Cookies: Cookies That Expire After X Seconds
A cookie that expires after a certain amount of time is coded like a session cookie, except that you must include an expiration date. However, the way you create the expiration date is a little odd. You need to create the expiration date in secondsnot only that, but it has to be in seconds since January 1, 1970. You may wonder how you are going to figure out your expiration dates when you have to determine them in regard to January 1, 1970. This is where the time() function comes in.
The time() function returns the number of seconds since January 1, 1970. If you want to create a cookie that expires in 30 days, you need to do the following:
Get the number of seconds since 1970.
Determine the number of seconds that you want the cookie to last.
Add the number of seconds since 1970 to the number of seconds that you want the cookie to last.
Because we know that there are 86,400 seconds in a day (60 seconds x 60 minutes x 24 hours), you could create a cookie that expires in 30 days, like this:
setcookie("username", "chris", time() + (86400 * 30));
This function places a cookie on the user's browser for 30 days. Anytime during those 30 days, you can access the variable $username from within the script and it will return (in the above example) chris.
cookie2.php
Create the script in Listing 1 (without the line numbers) in a text editor and name it cookie2.php. This script works much like cookie1.php, except that, in addition to storing the user's first name in a cookie, it also stores the date that the user last visited the page. It stores both of these values for 30 days from the time the user last accessed the page.
Listing 1: cookie2.php
1. <? 2. if(isset($f_name)) { 3. /* reset the cookie if there was a cookie previously */ 4. setcookie("f_name",$f_name, time() + 365 * 86400); 5. /* set the old date to the date from the cookie */ 6. if(isset($date)) { 7. $old_date = $date; 8. } else { 9. $old_date = date("l, F j Y"); 10. } 11. $date = date("l, F j Y"); 12. /* reset the date in the cookie to today */ 13. setcookie("date", $date, time() + 365 * 86400); 14. } 15. ?> 16. <html> 17. <head> 18. <title>The Cookie Script</title> 19. </head> 20. <body> 21. <? 22. if(isset($f_name)) { 23. ?> 24. <h3>Welcome back to the page <? print $f_name ?>! 25. <br>You were last here on <? print $old_date ?>.</h3> 26. <? 27. } else { 28. ?> 29. <form action="cookie2.php" method="POST"> 30. Tell us your First Name: <input type="text" name="f_name"> 31. <input type="submit" name="submit" value="Submit"> 32. </form> 33. <? 34. } 35. ?> 36. </body> 37. </html>
How the Script Works
The following table provides a line-by-line description of listing one.
1 |
This is the PHP start tag. For this script, you need to go right into PHP because you have to set any cookies before you send text to the browser. |
2 |
This line checks to see if the $f_name variable is set. The $f_name variable could have been set from the form, or a pre-existing cookie from a previous visit to the page may have set it. If the variable has not been set, then the script continues without doing anything. |
4 |
This line sets the cookie for the $f_name variable. You use the time function to get the current time and add that to one year. This allows the cookie to stay on the user's browser for one year or until the user deletes the cookie. |
610 |
If the $date variable is set (in the cookie), then the script sets the value of the $old_date variable to the value of the $date variable (from the cookie). This is done so that you can reference the last time the user was on the page. If the $date variable was not set in the cookie, then the $old_date variable is just set to today's date. |
11 |
This line sets the $date variable to the current date. |
13 |
This line sets the cookie for the date variable. Just like the $f_name variable, you set it to expire in one year. |
14 |
This line ends the if/then/else statement that started on line 2. |
15 |
This is the PHP end tag. |
1720 |
These lines print out the plain HTML headers for the page. |
22 |
This line checks to see if the $f_name variable has been set. |
2425 |
If the $f_name variable has been set, then you greet the user with a customized message stating the day that they last accessed the page. |
2732 |
If the $f_name variable has not been set, then the script prints out a form asking the user to enter his first name. |
34 |
This is line ends the if/then/else statement that started on line 22. |
3537 |
These lines end the PHP part of the page and print out the closing HTML tags. |