Talking about PHP cookie processing function, talking about Phpcookie function
(O゜▽゜) o☆[bingo!]
OK, let's see what cookies are.
A cookie is a small file that the server leaves on the client to identify the user or store some data (note that the session is stored on the server side, which is one of the differences). We usually log in to a portal, there will be the option "Save Login" or "Next automatic login", when we tick, the server will create a cookie file in our browser to save our information. Whenever a computer requests a page through a browser, it sends a cookie at the same time. With PHP, you can create and retrieve the value of a cookie. Cookies are a very important role in the web, and cookies are created as early as in Netscape's browser. Cookies are often used for user authentication systems.
1. Create a cookie
The function Setcookie () can generate cookies in PHP. Since the cookie is the content of the HTTP header section, you must call Setcookie () before outputting any data, which is similar to the header (), and defines:
BOOL Setcookie (string name[,string value][,int expire][,string path][,string domain][,bool secure][,bool HttpOnly])
A lot of parameters Ah! Σ (⊙▽⊙ "A is not urgent, let's look at the role of each parameter:
Name: Required, indicates the name of the cookie.
Value: Optional, represents the cookie value, stored in the client, when empty, means to revoke the information of the cookie in the client (so that the cookie can be deleted).
Expire: Optional, indicates the effective cutoff time of the cookie, which is the expiration time, if not specified or specified as 0, is usually invalidated when the browser is closed.
Path: optional, cookie valid path.
Domain: Optional, cookie valid domain name.
Secure: Indicates that the secure transport of HTTPS is valid.
Instance (create a cookie named Test with the value of China):
<?phpsetcookie ("Test", "China");? >
The value given by using Setcookie () can only be a number or a string , not another complex structure.
2. Obtaining cookies
Once you have created a cookie, you can use the predefined variable $_cookie to get the cookie. However, you can only get cookies on other pages, because in PHP, the cookie that is set does not take effect on this page unless the page is refreshed .
Instance:
<?php Setcookie ("Test", "China"); echo "Cookie is". $_cookie["Test"];?>
The reason here is to refresh the page is because the value of the cookie is not stored immediately after the call to Setcookie () C O O K I E Change Volume in , and is a with the h T T P Please Ask was Hair Send to Service Services Device , it when Hou C o o k I e of the value just will be out now in the "Ismathjax=" true "jaxid=" Html-css "style=" position:relative; Display:inline-block "> C OOKIEChangeVolumein,andis awiththehTTPPleaseAskwasHairSendtoServiceServicesDevice,itwhenHouCookIeof thevaluejustwill beoutnowin the The _cookie.
Expiration date of 3.cookie
A cookie has a life cycle, which is the effective time that a cookie exists. You can set the third parameter to set a valid time.
Instance (several ways to set a cookie's validity time):
Setcookie ("Cookie_one", "A", Time () +60*60); The cookie expires after one hour Setcookie ("Cookie_two", "B", Time () +60*60*24); Cookies expire after one day Setcookie ("Cookie_three", "C", Mktime (23,53,19,10,09,2020)); Cookies expire on October 9, 2020 23:53 19 seconds Setcookie ("Cookie_four", "D");
Valid path for 4.cookie
The path in the cookie is used to control which path the cookie is set to be valid, and the default is '/', which is valid under all paths, that is, the entire server domain name, and when the other path is set, it is only valid under the set path and sub-path, for example:
Setcookie (' Test ', Time (), 0, '/path ');
The above settings will make test valid under/path and sub-path/PATH/ABC, but the cookie value of test cannot be read in the root directory.
In general, most of the use of all paths, only in a very small number of special needs, will set the path, in this case only in the specified path to pass the cookie value, can save data transmission, enhance security and improve performance.
5. Delete Cookies
Deleting a cookie is simpler and is done by Setcookie () (Do not use unset ()!!! The following code is a simple example:
Setcookie ("Test", "");
The purpose of deleting a cookie is to set the second parameter to null . If you set a cookie with a specific value for the cookie, you still need to provide these parameters when you delete the cookie so that PHP can delete the cookie correctly.
This article on the PHP cookie processing function is a small part of the whole content to share to everyone, I hope to give you a reference, but also hope that we have a lot of support to help guests home.
http://www.bkjia.com/PHPjc/1135011.html www.bkjia.com true http://www.bkjia.com/PHPjc/1135011.html techarticle talking about PHP cookie processing function, talking about Phpcookie function (O゜▽゜) o☆[bingo!] OK, let's see what cookies are. A cookie is a server that is left on the client to identify the user or ...