What is a Cookie?

PHP transparently supports HTTP cookies. Cookies are a mechanism for storing data in the remote browser and thus tracking or identifying return users.

Creating Cookies With PHP

A cookie is created with the setcookie() function.

Syntax

setcookie(name, value, expire, path, domain, secure, httponly);

Creating a cookie

The following example creates a cookie named name with the value value. The cookie will expire after $int time.

<?php 
setcookie("name","value",time()+$int);
?>

Getting a cookie

<?php 
echo $_COOKIE["your cookie name"];
?>

Modifying a cookie

Check the following example -

<?php 
setcookie("color","red");
echo $_COOKIE["color"];
setcookie("color","blue");
echo $_COOKIE["color"];
?>

Deleting a cookie

To delete a cookie, use the setcookie() function with an expiration date in the past OR use unset() function like -

<?php 
unset($_COOKIE["yourcookie"]);
/*Or*/
setcookie("yourcookie","yourvalue",time()-1);
?>