Reader Level:

Working with cookies in PHP

By Mike Steward on Dec 12 2011
Today I will discuss about cookies in PHP which, in short "cookies are small strings of data created by a Web server but stored on the client".
    • Like
    • Love It
    • Awesome
    • Interesting
    • It's Okay
    • Thumbs Down
  • 11.4k
  • 0

Cookies in PHP

Hi guys, In this article we are going learn how to deal with cookies; first of all we need to know "what is the meaning of cookies" ?

A cookie is object used to identify the user by his/her computer. Cookies are small strings of data created by a Web server but stored on the client. In addition to having names and values, cookies have an expiration time. Cookies are often used to track user information.


A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too.

For this purpose you must install a XAMPP server.

Creating Your First PHP Cookie

For creating the very first cookies with PHP, we used the setcookie() function. This is a built in function in php.



Now we will understand the meaning of these arguments, the explanation of name, value and expiration are given below:

  • name: The name of your cookie. You will use this name to later retrieve your cookie, so don't forget it.
  • value: The value that is stored in your cookie. Common values are username(string) and last visit(date).
  • expiration: The date when the cookie will expire and be deleted. If you do not set this expiration date, then it will be treated as a session cookie and be removed when the browser is restarted.

This is the PHP script to set the cookies for the particular user :

head><title>PHP cookies info</title>
body bgcolor="cyan">
h3> Setting up cookies</h3><hr>
p> setcookie('lastVisit', date("G:i - m/d/y"), $oneMonth) </p><br>
p>with in php scripting<hr>
//Calculate 30 days in the future<br>
//seconds * minutes * hours * days + current time <br>
$oneMonth = 60 * 60 * 24 * 30 + time(); <br>
setcookie('lastVisit', date("G:i - m/d/y"), $oneMonth);<hr>
<p> Cookies enabled............</p>
$oneMonth = 60 * 60 * 24 * 30 + time();
'lastVisit', date("G:i - m/d/y"), $oneMonth);

Save it as abc.php.

Output of above php scripting

Type http://localhost/yourfoldername/function.php at your browser, here your output look like:


PHP script to retrieve the information

title>PHP cookies info</title>
body bgcolor="orange">
      $visit = $_COOKIE[
" you got stale cookies!";
echo "Welcome back! <br> You last visited on ". $visit;

Save it as ref.php.


Type http://localhost/yourfoldername/function.php at your browser, here your output look like :


Destroying the cookies

To destroy the cookie, simply use setcookie again, only set the expiration date to be in the past. Here is an example:

$past = time() - 60;
//this makes the time 60 seconds ago
setcookie(lastVisit, date("G:i - m/d/y"), $past);

Remember: Cookies need to be set in the header. This means they must be sent before any HTML is set to the page, or they will not work. Thanks !!