PHP Cookies

PHP Cookie is a small text of information that is created at the server-side but store in the client browser. Typically the size of the cookie is around 4KB.

Cookies are mainly used for tracking purposes like storing the information of name, age, or identification number so that whenever the visitor visits the webpage next time, the page can be personalized as per the stored information.

Every time the browser sends the request to the server, all the data in the cookie is automatically sent to the server within the request.

Setting a cookie in PHP

The setcookie() function is used to set the cookie in PHP. This function accepts up to six arguments and needs to call before the <HTML> tag otherwise the cookie will be not set.

Syntax

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

Here are the details of all the arguments.

Parameter Description
name The name of the cookie and is stored in an environment variable called HTTP_COOKIE_VARS
value The value of the cookie. Do not store sensitive information since this value is stored on the user’s computer.
expires The expiry date in UNIX timestamp format. After this time cookie will become inaccessible. The default value is 0.
path Specify the path on the server for which the cookie will be available. If set to /, the cookie will be available within the entire domain.
domain Specify the domain for which the cookie is available to e.g www.example.com.
secure This field, if present, indicates that the cookie should be sent only if a secure HTTPS connection exists.

Example

Below is the example of setcookie() function. Here, we will create a cookie of named username and assign the value as Mr. Phillip to it. We will also set the expire duration if cookie to 14 days (14 days * 24 hours * 60 min * 60 sec).

<?php
// Setting up a cookie
setcookie("username", "Mr. Phillip", time()+14*24*60*60);
?>

Accessing Cookie value in PHP

PHP provides several ways to access cookie values. Typically, cookies are accessed via $_COOKIE or $HTTP_COOKIE_VARS variables. The individual cookie value can be accessed using standard array notation, for example, to display the username cookie set in the previous example, you could use the following code.

<?php
// Accessing an individual cookie value
echo $_COOKIE["username"];
?>

Output

Mr. Phillip

It’s always a good practice to check whether a cookie is set or not before accessing its value. To do this you can use the PHP isset() function as below:

<?php
// Verifying whether a cookie is set or not
if(isset($_COOKIE["username"])){
    echo "Hello " . $_COOKIE["username"] . " wecome back.";
} else{
    echo "Welcome Guest!";
}
?>

You can also use printe_r() function to check the structure of $_COOKIE like other arrays.

Deleting Cookie in PHP

We can delete a cookie by calling the same setcookie() function with the cookie name only but this does not always work well always and should not be relied on.

The safest way to delete the cookie is to set the expiration date to the past as shown below.

Example

<?php
   // Deleting a cookie
   setcookie("username", "", time()-1800);
?>

You need to pass the same path, domain, and other arguments as passed while cookie has been created to ensure that correct cookie has been deleted.

Please get connected & share!

Advertisement