How to Create, Access and Delete Cookies in PHP

Cookies are small text files that are created by the server and stored on the user computer in around 4KB files. Cookies are used to store some information when user visit the website we set the cookies and next time when the user comes to the website we can retrieve the value of the cookie and manipulate the information or data for that particular user as per requirement, so cookies are very helpful in the website and we can use it in multiple scenarios. in this tutorial, we will let you know How to use cookies in PHP and How to Set Cookies, Get Cookies, Delete Cookies with PHP.

Uses of cookies

Cookies can be used for different purpose:

  • Store data like (font size, color, element properties, theme, or anything about a particular user, etc.)
  • Track something like (page count, percentage, behavior, etc.)
  • Store username or numbers (to validate the user)

Set Cookie

We can create a cookie using PHP setcookie(). This function needs mainly 3 parameters to create a cookie, name, value, and expiration time.

$cookie_val = 'atcodex'; //variable 
setcookie("websiteName", $cookie_val, time()+3600); //cookie name, value & time (1 hour)

Note: The setcookie() function must appear before the <html> tag.

If you run the code, it should create a cookie named “websiteName” with its value “atcodex” on the user computer, cookies expire time has set 1 hour so after an hour it will automatically be expired. In the same way we can set multiple cookies according to our requirements in website.

You can set more advanced parameters, which include path, domain, secure, httponly. setcookie(NAME, VALUE, EXPIRE, PATH, DOMAIN, SECURE, HTTPONLY)

$cookie_val = 'atcodex'; //variable 
setcookie("websiteName", $cookie_val, time()+3600, '/', '', true, true);

We have set ‘/’ in the path which means that cookies will be available in the entire domain. You can set cookies in ‘/folderName’ and then the cookie will available only in that folder and its sub-directories.

Retrieve Cookie Value

After setting the cookie value we can easily retrieve the cookie’s value on any page within the website using  $_COOKIE . The below code will show the cookie’s value in the browser.

echo 'Your Website Name Is ' . $_COOKIE['websiteName'];

Once cookies stored in the browser, it remain in the borswer till the expiration date or user deliberatly clear it in broswer setting. But if you want to delete cookies you do with just simple code shown in belwo example.

Delete a Cookie

To delete the stored cookie, just set the expiration date to negative.

setcookie("websiteName", "", time()-3600); //expiration time set to one hour ago

When you run this code, it will set the cookie to before time which triggers the browser cookie removal mechanism, which instantly removes cookie from the browser.

Updating Cookie in PHP

To update/modify a cookie value, simply just set its value again. like if we want to update the ‘websiteName‘, just change the name parameter using setcookie() method again.

$cookie_val = 'atcodex2'; //variable 
setcookie("websiteName", $cookie_val, time()+3600); //cookie name, value & time (1 hour)

Delete all cookies php

if we want to delete all the cookies at one go then just run the below code, it will unset all of the cookies on your domain

// unset cookies all at one go
if (isset($_SERVER['HTTP_COOKIE'])) {
    $cookies = explode(';', $_SERVER['HTTP_COOKIE']);
    foreach($cookies as $cookie) {
        $parts = explode('=', $cookie);
        $name = trim($parts[0]);
        setcookie($name, '', time()-3600);
        setcookie($name, '', time()-3600, '/');

How to store Array and JSON in Cookie

Sometimes we need to store array in a cookie, for that, we need to define all the value in the array and just use json_encode to encode that array in JSON and store in a cookie.

Store JSON value in Cookie

setcookie('cookieData', json_encode($arrayData));

Store array value in Cookie

setcookie ("atcodex[name]", $name);
setcookie ("atcodex[city]", $city);
setcookie ("atcodex[state]", $state);
setcookie ("atcodex[zip]", $zip);

How to Retrieve Array and JSON from Cookie

Get value of stored JSON

$arrayData = json_decode($_COOKIE['cookieData']);

Get value of stored array