Sunday, 9 June 2013

How to Delete Cookies in PHP


Cookies are nothing but small text files stored on client computers via browser.  Cookies generally contain user information like userid, sessionid etc.

Main use of cookie is to store the user information permanently on the client computer. So that when that client visits the site sometime in future, Server knows who that client is and can customize the view of the webpage.

In php you can set the cookie as well as delete it.

Syntax:
setcookie(cname, cvalue, cexpire, cpath, cdomain);

As shown above, we can use setcookie function to set the cookie with name cname and value as cvalue.

We can also set the expiry date of the cookie.

Below program, I am accessing cookie with name myuser

if (isset($_COOKIE["myuser"]))
  echo "Hi " . $_COOKIE["myuser"] ;
else
  echo "Hi guest!";


To delete the cookie you can set the expiry time of the cookie to past.

<?php
// set the expiration date to one hour ago
setcookie("myuser", "", time()-6600);

?>



This is how we can set the cookie and delete it in PHP.

No comments:

Post a Comment