$_COOKIE in PHP

What is $_COOKIE in PHP ?

$_COOKIE  is a superglobal variable used to handel cookie in PHP. 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 in PHP.

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

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

Cookies 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 cookies in PHP /

Deleting Cookie

 1.unset($_COOKIE[“KeyName”]);


2.setcookie(“SomeKey”,“KeyValue”,time()-1);
it expired ie. it’s deleted