A cookie is used to store data on a users computer. Creating and retrieving cookies and their values is an easy process in PHP and in this short guide I am going to show you how to use cookies.
Creating a cookie
The Code
setcookie(”cookieName”, “cookieValue”, cookieExpirationTime);
an example:
setcookie(”visitor”, “seenPage”, time()+3600);
this creates a cookie called visitor with the value seenPage and it will expire in 3600 seconds (or an hour)
Finding a cookies value
The Code
echo $_COOKIE[”cookieName”];
an example:
echo $_COOKIE[”visitor”];
this will show to screen the value that is stored in the cookie with the name visitor, so the result would be “seenPage” if we use the previous example
Deleting a cookie
The Code
setcookie(”cookieName”, “”, timeExpirationTime);
an example:
setcookie(”visitor”, “”, time()-3600);
you can see that the time()-3600 is similar to when you create a cookie, this is because if you replace the + with a - then it cancels out the cookie.
So there you have it a very simple quick guide to PHP cookies, I hope this helped