Rate Us :

Share with Friends :


PHP-Cookies :

Cookie stores the data in clients memory location. These data can be access from any web page within the application. Cookies stores the information in two locations. Either in Hard Disk or in RAM memory location of client system.

Cookies are divided into two types :-

  • Inmemory Cookie

  • Persistence Cookie

Inmemory Cookie : (stores information in RAM)

If we create any cookie without explicit Expiry tag then it comes under inmemory cookies. Inmemory cookie stores the information in clients RAM memory location and destroys the data when user close the browser.

Persistence Cookie : (stores information in Hard Disk)

If we create any cookie with explicit Expiry tag comes under persistence cookie. Persistence cookie stores the data in Hard Disk and deletes the information when the lifetime of cookies is completed.

setcookie() :

By using this function we can create the cookies in PHP.


By using this global variable we can get the value of cookie. Cookie stored in browser memory location that's why we cannot access the cookie from one browser to another browser. Cookies are browser dependent.

Example :


<?php setcookie('x', 100); echo "cookie created"; ?> <a href="Page2.php">GO</a>


<?php echo "value is".$_COOKIE['x']; ?>

Output :

cookie created GO

Steps to create persistence cookie :

  • Get the current date and time information when user send the request to access the file where we created cookies.

  • Add lifetime to the current date and time to get Expiry time.

  • Create cookies with that Expiry tag.

Example :

<?php setcookie("un", "Man"); setcookie("n", 1001, time90+3600); echo $_COOKIE['un']; echo $_COOKIE['n']; ?>

Output :

Man 1001
(If we set time like 3600 sec. means one hour, the output will be same upto one hour)
  • Persistence cookies will store in Hard Disk in a file of browser memory location. The name of that file is username@domain_name.

  • We can delete the cookie from client system by recreating the cookie with completed time.

  • Disadvantages of Cookies :

  • Cookies stores the information in client system that's why client can delete the information or client can modify cookie data.

  • Cookie is storing limited amount of data.

  • Cookie can store only text data.