Cookies in PHP

What is $_COOKIE in PHP ?

Cookies in PHP ( $_COOKIE  [] )is a superglobal variable used to handel cookie . 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. With PHP, you can both create and retrieve cookie values.To Retrieve the values of cookie we use $_COOKIE .

  
Syntax : $variable = $_COOKIE  [‘SomeKey’];
 

Where as a cookie is created with the setcookie() function.

Cookie in PHP with examples

To create Cookie:

 
setcookie(\"SomeKey\",\"KeyValue\",time()+$int);
name is your cookie\'s name
value is cookie\'s value
$int is time of cookie expires  

Fetch Cookie:

  

echo $_COOKIE[\"SomeKey\"];

  

Updating Cookie:

  

setcookie(\"color\",\"red\");
echo $_COOKIE[\"color\"];
/*color is red*/
/* your codes and functions*/
setcookie(\"color\",\"blue\");
echo $_COOKIE[\"color\"];
/*new color is blue*/
  

 UNSET cookie in PHP

Deleting Cookie

 * unset($_COOKIE[\”KeyName\”]);


 * setcookie(\”SomeKey\”,\”KeyValue\”,time()-1);


it expired ie. it\’s deleted

Scroll to Top