Usage of cookies in PHP (easy to understand) and usage of cookies
Use of cookies in PHP --- add/update/delete/obtain cookies, automatically enter the user name and password, and determine whether to log on for the first time
What is cookie?
The server saves user information on the client, such as the login name and password.
The data is like a cookies. The data volume is not large. The server can read the data from the client as needed and save it in the browser cache directory of the client.
① When accessed by a browser, cookie. in php, the server also uses Set-Cookie: name = zxf; expire = Wed, 21-Sep-2017 GMT to send an http response. After the browser obtains the message, the cookie information will be saved to the local disk.
② If we do not have time (the third parameter)
The cookie is not saved to the client. When the browser session ends, the cookie becomes invalid.
③ Cookie stores string Information
④ The client can save multiple keys => val
⑤ When the cookie is stored, the URL encoding is performed for Chinese characters.
Cookie can have multiple keys => val, and different key values can be given to set different effective times.
The Code is as follows: xx. php
<? Php // Add cookiesetcookie ("name", "zxf", time () + 3600); // array/$ arr = array (, 3 ); $ arr_str = serialize ($ arr); setcookie ("a", $ arr_str, time () + 3600); // obtain the cookie var_dump ($ _ COOKIE ); // update cookiesetcookie ("name", "aaa", time () + 3600); // Delete cookiesetcookie ("name", "", time ()-20 ); // Delete All foreach ($ _ COOKIE as $ key = >$ value) {setcookie ($ key, "", time ()-1 );} echo "successful";?>
If the key => val of the cookie you deleted is not deleted, the cookie is retained on the client. If all the cookies of the website are deleted, the browser deletes the cookie file.
Determine whether to log on for the first time
<? Php // first determine whether the cookie contains the previous login information if (! Empty ($ _ COOKIE ['lastvisit']) {echo "The Last Time You logged in ". $ _ COOKIE ['lastviat']; // Update time setcookie ("lastVisit", "data (Y-m-d H: I: s)", time () + 3600);} else {// indicates that the user is logging on to echo for the first time. "// The Update Time setcookie (" lastViait ", "data (" Y-m-d H: I: s ")", time () + 3600) ;}?>
When the login interface is opened, the user name and password of the user are automatically entered.
Checklogin. php
// Obtain whether the idif (! Empty ($ _ POST ['cooker']) {setcookie ("id", $ id, time ()-100);} else {if (! Empty ($ _ COOKIE ['id']) {setcookie ("id", $ id, time ()-10 );}}
The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!