English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn how to store a small amount of information inside the user's browser using PHP cookies.
A cookie is a small text file that allows you to store a small amount of data on the user's computer (up to4about KB (kilobytes). They are usually used to track information such as usernames, so that when the user visits the website again, the website can retrieve this information to display a specific personalized page.
Tip:Every time the browser requests a page from the server, all the data in the cookie is automatically sent to the server within the request.
The setcookie() function is used to set cookies in PHP. Make sure to call the setcookie() function before any output is generated by the script, otherwise the cookie will not be set. The basic syntax of the function can be given as follows:
setcookie(name, value, expire, path, domain, secure);
The parameters of the setcookie() function have the following meanings:
Parameters | Description |
---|---|
name | The name of the cookie. |
value | The value of the cookie. Since this value is stored on the user's computer, do not store sensitive information. |
expires | UNIX timestamp format expiration date. After this time, the cookie will become inaccessible. The default value is 0 |
path | Specify the path on the server where the cookie is available. If set to/Then the cookie will be available throughout the domain. |
domain | Specify the domain that is available for the cookie, for example: www.oldtoolbag.com. |
secure | This field (if it exists) indicates that the cookie will only be sent if there is a secure HTTPS connection. |
Tip:If the cookie expiration time is set to 0 or omitted, the cookie will expire at the end of the session, that is, when the browser is closed.
Here is an example of creating a cookie named 'userName' using the setcookie() function and assigning it the value 'John Carter'. At the same time, the cookie expiration time is specified as30 days (}}30 days * 24 hours * 60 min * 60 sec).
<?php //Set Cookie setcookie("username", "John Carter", time())+30*24*60*60); ?>
Note:All parameters except the name are optional. You can also use an empty string ("") to replace a parameter to skip it, but to skip the expire parameter, use zero, as it is an integer.
Warning:Do not store sensitive data in Cookies, as malicious users may manipulate sensitive data. To securely store sensitive data, useSession.
The PHP superglobal variable $_COOKIE is used to retrieve cookie values. It is typically an associative array containing a list of all cookie values sent by the browser in the current request, with cookie names as keys. You can access a single cookie value using standard array notation, for example, to display the username cookie set in the previous example, you can use the following code.
<?php //Access a single Cookie value echo $_COOKIE["username"]; ?>
The PHP code in the above example produces the following output.
John Carter
It is best to check if the cookie is set before accessing the value of the cookie. For this purpose, you can use the PHP isset() function, as shown below:
<?php //Verify if the cookie is set if (isset($_COOKIE["username"])) { echo "Hi " . $_COOKIE["username"]; } else { echo "Welcome Guest!"; } ?>
You can use the print_r() function like print_r($_cookie) to view the structure of the $_cookie associative array, just like you would handle other arrays.
You can delete a cookie by calling the same setcookie() function with the cookie name and any value (such as an empty string), but you need to set it to an expiration date before now, as shown in the following example:
<?php //Delete cookie setcookie("username", "", time())-3600); ?>
Tip:You should pass the same name as the path, domain, and other parameters used when the Cookie was first created to ensure that the correct Cookie is deleted.