Creating a cookie
A cookie can be created using the setcookie function in PHP.

setcookie($name, $value, $expire, $path, $domain, $secure)

  • $name – name of the cookie.
  • $value – value of the cookie.
  • $expire – time when the cookie will expire.
  • $path – path on the server where cookie will be available.
    If the path is set to “/”, the cookie will be available through out the whole site.
    If no path is given, cookie in created under the current directory.
  • $domain – domain where cookie will be available. Instead of path you can use domain settings.
  • $secure – true if cookie is being set over a secure “https” server, false otherwise, Default value is false.

setcookie(“username”, “john”, time()+(60*60*24*365); // this cookies last for one year

Retrieving a cookie

echo $_COOKIE["username"]; //return “john”

Deleting a cookie
To delete cookies, just set the cookie to expire in the past date

setcookie(“username”, “john”, time()-(60*60*24*365));