PHP Cookie plays an important role in PHP caching. This article provides some detailed explanations of its related knowledge.
What are Cookies?
Cookies are often used to identify users. A cookie is a small file that a server leaves on a user's computer. Each time the same computer requests a page through the browser, the cookie will be sent to the computer. With PHP, you can create and retrieve cookie values.
How to create a cookie?
The setcookie() function is used to set cookies.
Note: The setcookie() function must be located before thetag.
Syntax
setcookie(name, value, expire, path, domain);
Example 1
In the following example, we will create a cookie named "user" and assign it the value "runoob". We also specify that this cookie expires after one hour:
.....
Note: The cookie value is automatically URL-encoded when sending the cookie and automatically decoded when retrieved. (To prevent URL encoding, use setrawcookie() instead.)
Example 2
You can also set the cookie expiration time in another way. This may be simpler than using seconds.
.....
In the above example, the expiration time is set to one month (60 seconds * 60 minutes * 24 hours * 30 days).
How to retrieve the value of Cookie?
PHP’s $_COOKIE variable is used to retrieve the value of the cookie.
In the following example, we retrieve the value of the cookie named "user" and display it on the page:
In the following example, we use isset() function to confirm whether the cookie has been set:
How todeleteCookie?
When deleting a cookie, you should change the expiration date to a point in time in the past.
Deleted example:
What should I do if my browser does not support Cookies?
If your application needs to deal with browsers that do not support cookies, then you will have to use other methods to pass information between pages in your application. One way is to pass data through a form (forms and user input are covered in the previous chapters of this tutorial).
The following form submits user input to "welcome.php" when the user clicks the "Submit" button:
Retrieve the value in the "welcome.php" file, as follows Shown:
This article provides a detailed understanding of Cookie-related knowledge. For more learning materials, please pay attention to the php Chinese website to view.
Related recommendations:
PHP Session’s understanding and application of cache-related knowledge
About PHP array sorting Knowledge application
#Related knowledge and application of PHP file upload
The above is the detailed content of Understanding and using PHP Cookie related knowledge. For more information, please follow other related articles on the PHP Chinese website!