Cookies in PHP

What is a Cookie?

Cookie is a global constant in PHP. Cookies are helpful to store user data at client-side for a long time. It can be accessed through any PHP file within the web application.

Mostly cookies are used to store user login details for a long time. Even more, advertisers use cookies to store user data for a long time.

Set a Cookie

We can set cookies using setcookie() function in PHP.

Syntax

setcookie(COOKIE_NAME, COOKIE_VALUE, COOKIE_DURATION)

Explanation

Here only Name field is required and all other fields are optional.

  • COOKIE_NAME – Cookie name we want to set
  • COOKIE_VALUE – Cookie value we want to store
  • COOKIE_DURATION – Cookie validity. Cookies will be automatically destroyed after the cookie duration is over.

Note: There are more optional fields in setcookie() function like path, domain, secure and httponly. However, they are quite less popular in usage and only used when required.

Read Also: Session vs Cookies in PHP

Access/Retrieve a Cookie

Cookie is a global constant in PHP. So, we can access cookies in any webpage all over the web application using the below syntax.

Syntax

$_COOKIE['COOKIE_NAME'])

Explanation

In the above syntax, COOKIE_NAME is the name of the cookie we want to retrieve from local storage.

Example

<?php
$cookie_name = 'Website';
$cookie_value = 'errorsea.com';

setcookie($cookie_name, $cookie_value, time() + (86400 * 30));
// 86400 * 30 = 30 days

if(!isset($_COOKIE['$cookie_name'])) {
     echo "Cookie '" . $cookie_name . "' is not available.";
} else {
     echo "Cookie '" . $cookie_name . "' is available.<br>";
     echo "Cookie value is: " . $_COOKIE[$cookie_name];
}
?>

Output

Cookie 'Website' is available.
Cookie value is: errorsea.com

Modify a Cookie

To modify a cookie we just have to set a new cookie with the same cookie name we want to modify. It will simply overwrite the old cookie with the new one.

Syntax

setcookie(COOKIE_NAME, COOKIE_NEW_VALUE, COOKIE_DURATION)

Explanation

In the above syntax, we used the setcookie() function to modify a PHP cookie which we used to create a cookie. The only parameter we changed is a COOKIE_VALUE with COOKIE_NEW_VALUE to modify a cookie with a new value.

Note: We need to set the exact name of cookie we want to modify otherwise system will create a new cookie.

Example

<?php
$cookie_name = 'Website';
$cookie_value = 'https://errorsea.com';

setcookie($cookie_name, $cookie_value, time() + (86400 * 30));
// Modify a cookie value from errorsea.com to https://errorsea.com

if(!isset($_COOKIE['$cookie_name'])) {
     echo "Cookie '" . $cookie_name . "' is not available.";
} else {
     echo "Cookie '" . $cookie_name . "' is available.<br>";
     echo "Cookie value is: " . $_COOKIE[$cookie_name];
}
?>

Output

Cookie 'Website' is available.
Cookie value is: https://errorsea.com

Delete a Cookie

There is no direct solution to delete a cookie in PHP. However, there is still a way to delete a specific cookie.

We can use setcookie() function to delete a cookie. We can modify a Cookie with empty value and past duration.

Syntax

setcookie(COOKIE_NAME, '', time() - 3600)

Explanation

Here we modified a cookie with empty value and an expired time to delete a cookie.

Example

<?php
$cookie_name = 'Website';
$cookie_value = '';

setcookie($cookie_name, $cookie_value, '');
// Delete a cookie

if(!isset($_COOKIE['$cookie_name'])) {
     echo "Cookie '" . $cookie_name . "' is not available.";
} else {
     echo "Cookie '" . $cookie_name . "' is available.<br>";
     echo "Cookie value is: " . $_COOKIE[$cookie_name];
}
?>

Output

Cookie 'Website' is not available.

Conclusion

I hope now you have a straight forward understanding of cookies in PHP. Cookies are providing an important role in data storage at a client browser.  In addition, now you can easily apply cookies in your PHP project.

Happy Programming 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *