Like all other web languages, PHP also provides Cookies to store some data for a long time on the client-side.
What is a Cookie in PHP?
A cookie is a global constant in PHP. Cookies help store user data at the 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.
How to Set a Cookie in PHP?
We can set cookies using setcookie() function in PHP.
Syntax
setcookie(COOKIE_NAME, COOKIE_VALUE, COOKIE_DURATION)
Explanation
Here only the 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
How to Access/Retrieve a Cookie in PHP?
A cookie is a global constant in PHP. So, we can access cookies on 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
How to Modify a Cookie in PHP?
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 the cookie we want to modify; otherwise, the 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
How to Delete a Cookie in PHP?
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 an empty value and past duration.
Syntax
setcookie(COOKIE_NAME, '', time() - 3600)
Explanation
Here we modified a cookie with an 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’s browser. Also, now you can easily apply cookies in your PHP project.
Happy Programming 🙂