Usage of cookies in PHP (easy to understand) and usage of cookies

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.