Home >Backend Development >PHP Tutorial >How to use cookie function in PHP
In web development, cookies are a persistent storage mechanism that can track user status and allow websites to store user preferences and other information. In PHP, the cookie function can be used to easily operate cookies. This article will introduce how to use the cookie function in PHP.
1. Use the setcookie() function to set cookies
In PHP, the setcookie() function is used to set cookies. The setcookie() function has three required parameters and two optional parameters. The syntax is as follows:
setcookie(name, value, expire, path, domain, secure, httponly);
where name The parameter represents the name of the cookie; the value parameter represents the value of the cookie; the expire parameter represents the expiration time of the cookie, which is in UNIX timestamp format. You can use the time() function to obtain the current timestamp plus the length of time to be stored, such as time() 86400 It means to save it for one day; the path parameter indicates the action path of the cookie, which can be empty to indicate that it can be used in the entire website, or it can be used under a specified path; the domain parameter indicates the scope of the cookie. If it is not set, it means that it can be used under the current domain name. Use; the secure parameter indicates whether the cookie can only be transmitted through the HTTPS protocol, usually false; the httponly parameter indicates whether the cookie can only be accessed through the HTTP protocol, usually true.
For example, the following code sets a cookie named username, with a value of "John", an expiration time of one day, and an effect path of the entire website:
setcookie("username", "John", time()+86400, "/");
2. Use the $_COOKIE super global Variable reading cookie value
In PHP, the $_COOKIE superglobal variable is used to store the cookie information passed to the current script. $_COOKIE is an array, the key is the name of the cookie, and the value is the value of the cookie. For example, the following code can output the value of a cookie named username:
echo $_COOKIE["username"];
If the cookie does not exist, the output is empty.
3. Use the setcookie() function to delete cookies
In PHP, you can use the setcookie() function to delete cookies. In order to delete a cookie, you need to set the expiration time to a timestamp in the past, so the browser will delete the cookie.
For example, the following code deletes the cookie named username:
setcookie("username", "", time()-3600, "/");
Note that you need to specify the same path and domain name when deleting the cookie, otherwise the cookie cannot be deleted.
4. Summary
In PHP, using the cookie function can easily set, read and delete cookies. When using the setcookie() function to set cookies, you need to pay attention to parameter settings. When using the $_COOKIE superglobal variable to read the cookie value, you need to pay attention to whether the cookie exists. When using the setcookie() function to delete cookies, you need to pay attention to specifying the same path and domain name. Mastering the use of cookie functions can help us better control user status and improve the interactivity and personalization of the website.
The above is the detailed content of How to use cookie function in PHP. For more information, please follow other related articles on the PHP Chinese website!