PHP Cookie Examples (Setcookie, Remove & Check)

PHP Cookie Examples (Setcookie, Remove & Check)

HTTP is a stateless protocol. During Client Server communication to maintain state we use some techniques. Cookie is one of them. Generally we use Cookies to identify an user. When first time user send request to the server in response server sends a set of Cookies. Which browser stores in Client machine. Next time when the same user request again browser send the information store in Cookies. Through which server get to identify the user.

Cookie is a text file. It can store maximum upto 4mb data. While creating a Cookie its mandatory to provide Expiry date. Compare to session Cookies is not secured. For programmers it is advisable to not store secured data in Cookies. PHP is a server side programming language. In this session let us share Cookie related operations using PHP.

In PHP to create a Cookie there is a setcookie() method. setcookie accepts 6 parameters. The syntax to create Cookie in PHP is as below.

setcookie(name, value, expire, path, domain, security);

PHP setcookie() method parameters

name Using this parameter you can set the name of the Cookie. In future if you want to access value of the Cookie name is required. Physically Cookie name stored in an environment variable HTTP_COOKIE_VARS.
value Cookie stores data in key value pair. Key is the name where value is the real data.
expire While creating a Cookies we need to decide how long the cookie need to reside in Client machine. This can be set using expire property. By default if you will not set expire time cookie will get destroy after browser get close.
path Path specifies the physical directories for which the cookie is valid. Using single forward slash permits the cookie to be valid for all directories.
domain Using this property you can specify the domain name in very large domains. By default all cookies are only valid for the host & domain which created them.
security This parameter accepts 2 values 1 or 0. By passing 1 you can specify that cookie need to travel only by secure transmission (HTTPS). By passing 0 cookie can be travel using regular HTTP.

PHP Write Cookie Example using Setcookie

setcookie("empName", "Baby Roy", time()+3600, "/","", 0);
setcookie("empID", "996782", time()+3600, "/", "",  0);

How to print value of Cookies in PHP?

There are 2 methods to access Cookies in PHP. $_COOKIE & $HTTP_COOKIE_VARS.

echo $HTTP_COOKIE_VARS["empName"];
echo $_COOKIE["empID"];

PHP Check Cookie Example

In PHP there is a method isset() using this you can know whether there is a Cookie exists or not. Look at the example below.

if( isset($_COOKIE["empName"]))
echo "Welcome " . $_COOKIE["empName"];
echo "No Cookies";

PHP Remove Cookies Example

To delete a Cookie in PHP you required to set expiry time less than the current time. For an example refer to above we have two cookies empName & empID. To delete these cookies we required to call setcookie() method with expiry time less than the Current time. Look at the example below.

setcookie("empName", "Baby Roy", time()-60, "/","", 0);
setcookie("empID", "996782", time()-60, "/", "",  0);