Cookies used to store information about the site visitor on their computer to be accessed again the next time they visit. One common use of cookies is to store your username and password on your computer so you don’t need to login again each time you visit a website. Cookies can also store other things such as your name, last visit, shopping cart contents, etc.
A cookie is set with the following code:
setcookie(name, value, expiration)
For Example:
<?php $Month = 2592000 + time(); //this adds 30 days to the current time setcookie(enddate, date("F jS - g:i a"), $Month); ?>
The above code sets a cookie in the visitor’s browser called ” enddate “. The cookie sets the value to the current date, and set’s the expiration to be be in 30 days (2592000 = 60 seconds * 60 mins * 24 hours * 30 days.)
Retrieve Cookie Values:
<?php if(isset($_COOKIE['AboutVisit'])) { $last = $_COOKIE['AboutVisit']; echo "Welcome back! <br> You last visited on ". $last; } else { echo "Welcome to our site!"; } ?>
This code first checks if the cookie exists. If it does, it welcomes the user back and tells them when they last visited. If they are new, it skips this and prints a generic welcome message.