Like all other web languages, PHP also provides Cookies to store some data for a long time on the client-side.
Index
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.
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.
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
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.
<?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];
}
?>
Cookie 'Website' is available.
Cookie value is: errorsea.com
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.
<?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];
}
?>
Cookie 'Website' is available.
Cookie value is: https://errorsea.com
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.
<?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];
}
?>
Cookie 'Website' is not available.
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 🙂
There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…
In this tutorial, I'm going to show you how to install MySQL on your computer.…
Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…
We can redirect any webpage to any other or redirect the whole domain or website…
There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…
Include files in PHP are used in appending various global or config files. We can…