Super global variable $_COOKIE

A cookie is a small piece of information file that is embedded with the server and stored in the user’s computer browser. It is often used to identify users or the date that will be used for other sections of website. In each request, the user browser sends information from the user’s browser to the server. Hence cookie is created on the server and saved in the client browser. So, the cookie is created, sent, and received at the server end.

In PHP, a Cookie is an associative array of variables passed through the current script via HTTP cookies.

In PHP, setcookie() function is used for creating cookies.

Syntax for setcookie(name, value, expiree, path, domain, secure, http only);

The “name” parameter is a required parameter and the rest are optional.

After creating the cookie we will use the super global PHP variable $_COOKIE[] to access the cookie.

Note:- we can not use dot(.) and space( ) for cookie names. It should always be replaced with an underscore(_).



setcookie(“username”, “Sonoo”, “25000”);





if(!isset($_COOKIE[“username”])) {

echo “Sorry, cookie created.”;

} else {

echo “<br/>Cookie Value: ” . $_COOKIE[“username”];







setcookie(“cookie_name”, “KSHTUTOR”, time() + 2 * 24 * 60 * 60);

echo “Cookie name is a  ” . $_COOKIE[“cookie_name”];


To delete a cookie, we should pass past time in the time parameter of the setcookie() Function. Like this.

setcookie(“cookie_name”, “”, time() –  60);

Note:- If the time parameter is set to zero(0) or omitted the cookie will expire after the end of the session or closing of the browser.

Note:- The value of the cookie is automatically URL encoded when sending the cookie and automatically decoded when receiving the cookie.

Share with:

Leave a Reply

Connect with:

Verified by ExactMetrics