In this article we are going to cover Cookies in PHP with Examples,What are Cookies in PHP, Create Cookies with PHP, Modify a Cookie value in PHP, Delete a Cookie in PHP
Table of Contents
What are Cookies in PHP?
A cookie is a small text file that lets you store a small amount of data (nearly 4KB) on the users computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.
Create Cookies with PHP
A cookie is created with the setcookie() function. The setcookie() function needs to be called prior to any output generated by the script otherwise the cookie will not be set.
Syntax:
setcookie(name, value, expire, path, domain, secure, httponly);
Only the name parameter is required. All other parameters are optional.
Name: It is used to set the name of the cookie.
Value: It is used to set the value of the cookie.
Expire: It is used to set the expiry timestamp of the cookie after which the cookie can’t be accessed.
Path: It is used to specify the path on the server for which the cookie will be available.
Domain: It is used to specify the domain for which the cookie is available.
Secure: It is used to indicate that the cookie should be sent only if a secure HTTPS connection exists.
The following example creates a cookie named “user” with the value “demo”. The cookie will expire after 30 days. We retrieve the value of the cookie “user” using the global variable $_COOKIE. We also use the isset() function to find out if the cookie is set.
Example of create cookie in PHP:
<!DOCTYPE html>
<?php
$cookie_name = "user";
$cookie_value = "demo";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 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];
}
?>
</body>
</html>
Output:
Cookie 'user' is set!
Value is: demo
![Cookies in PHP with Examples [2 Steps] 1](https://www.fosstechnix.com/wp-content/uploads/2021/07/create-cookie_1-1.png)
Modify a Cookie value in PHP:
To modify a cookie, just set the cookie using the setcookie() function:
Example of modify a cookie value:
<!DOCTYPE html>
<?php
$cookie_name = "user";
$cookie_value = "cookie_demo";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
?>
<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];
}
?>
</body>
</html>
Output:
Cookie 'user' is set!
Value is: cookie_demo
![Cookies in PHP with Examples [2 Steps] 2](https://www.fosstechnix.com/wp-content/uploads/2021/07/modify-cookie_2.png)
Delete a Cookie in PHP:
To delete a cookie, use the setcookie() function with an expiration date in the past
Example of delete a cookie:
<!DOCTYPE html>
<?php
setcookie("user", "", time() - 3600);
?>
<html>
<body>
<?php
echo "Cookie 'user' is deleted.";
?>
</body>
</html>
Output:
Cookie 'user' is deleted.
![Cookies in PHP with Examples [2 Steps] 3](https://www.fosstechnix.com/wp-content/uploads/2021/07/delete-cookie_3.png)
Conclusion:
In this article we have have covered What are Cookies in PHP, Create Cookies with PHP, Modify a Cookie value in PHP, Delete a Cookie in PHP.
Related Articles:
- Introduction about PHP [PHP 7]
- Date and Time in PHP with Examples [2 Steps]
- 3 Data Types in PHP with Examples
- Form Validation in PHP with Examples
- 3 Types of Arrays in PHP with Examples
- 4 Types of Loop in PHP with Examples
- 4 Types of String in PHP with Examples
Reference: