A PHP cookie is a tiny file that the web server saves on the client computer, with a maximum size of 4KB. The cookie will be sent each time the same computer requests a page through a browser. They are often used to store track of information like a username that the site can use to customize the page when user revisits the site.
Cookies are typically set in an HTTP header, but JavaScript can also set a cookie on the browser directly.
Cookie values can be created and retrieved using PHP.
Create PHP Cookies
The setcookie() function is used to create a cookie. The setcookie() function should be called before any output produce by the script else the cookie will not be saved.
Syntax
setcookie(name, value, expire, path, domain, security);
Parameters of setcookie():
In general, the setcookie() function requires six arguments:
Parameter | Description |
Name | It is used to give the cookie a name. |
Value | It is used to set the cookie’s value. |
Expire | It is used to set the cookie’s expiry timestamp, after which the cookie can no longer be retrieved. |
Path | It is used to provide the server path where the cookie will be accessible. |
Domain | It is used to define that for which domain the cookie is valid. |
Security | It is used to specify that the cookie should only be transmitted if a secure HTTPS connection is available. |
Note: The name parameter is the only one that is required. The remaining parameters are all optional.
PHP Create/Retrieve a Cookie
The below example sets the value of a cookie named “user” to “Henry.” After 30 days (86400 * 30), the cookie will expire.
The value of the cookie “user” is then retrieved by using global variable $_COOKIE. We can also use the isset() function to see if a cookie has been set:
<!DOCTYPE html> <?php $cookie_name = "User"; $cookie_value = "Henry"; setcookie($cookie_name, $cookie_value, time() + (86400 * 30)); // 86400 means 1 day ?> <html> <body> <?php if(!isset($_COOKIE[$cookie_name])) { echo "Cookie named '" . $cookie_name . "' is not set!"; } else { echo "Cookie '" . $cookie_name . "' is set!<br>"; echo "Value is: " . $_COOKIE[$cookie_name]; } ?> <p><b>Note:</b> Reload the page if the value of the cookie is not set.</p> </body> </html>
Note: The setcookie() method must be called, before html tag.
Check if Cookies are Enabled
The example below shows chunk of code that checks if cookies are enabled.
To begin, use the setcookie() function to create a test cookie, then check the $_COOKIE array variable:
<!DOCTYPE html>
<?php
setcookie("test_cookie", "test", time() + 3600);
?>
<html>
<body>
<?php
if(count($_COOKIE) > 0) {
echo "Cookies are Enabled.";
} else {
echo "Cookies are Disabled.";
}
?>
</body>
</html>
Modify a Cookie Value
To make changes to a cookie, simply use the setcookie() function to re-set the cookie:
<!DOCTYPE html>
<?php
$cookie_name = "User";
$cookie_value = "Bean";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30)); // 86400 means 1 day
?>
<html>
<body>
<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>
<p><b>Note:</b> Reload the page if the value of the cookie is not set.</p>
</body>
</html>
Delete a Cookie
Use the setcookie() function with an expiration date to remove a cookie:
<!DOCTYPE html>
<?php
// set the expiration date to one hour ago
setcookie("User", "", time() - 50);
?>
<html>
<body>
<?php
echo "Cookie 'User' is deleted.";
?>
</body>
</html>