Use of cookies in PHP

Source: Internet
Author: User
Tags setcookie
This article mainly introduces the use of cookies in PHP: Add/Update/delete/Get cookies and automatically fill in the user's user name and password and determine whether the first landing, has a good reference value.

Use of cookies in PHP---Add/update/delete/Get cookies and automatically fill in the user's username and password and determine whether to log in for the first time

What is a cookie

The server saves the user's information on the client, such as login name, password, etc.

This data is like cookies, the amount of data is not large, the server can be read from the client when needed, saved in the client's browser cache directory

① when a browser accesses cookie.php, the server also sends an HTTP response at set-cookie:name=zxf;expire=wed,21-sep-2017 20:14 GMT, and when the browser obtains the message, The cookie information is saved to the local disk

② If we don't have time (the third parameter)

Cookies are not saved to the client, and the cookie expires when the browser session ends

③cookie The string information is saved

④ clients can save multiple Key=>val

⑤cookie in the save process, the Chinese will be urlencode encoded

A cookie can have multiple key=>val, which can give different key values and make different effective time.

The code is as follows: xx.php

<?php//add Cookiesetcookie ("name", "ZXF", Time () +3600);//array/$arr = Array (n/a);  $arr _str = serialize ($arr);  Setcookie ("A", $arr _str,time () +3600); Get 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 "Success";?>

If the key=>val of the cookie you deleted is not finished, then this cookie is reserved on the client and if the cookie of this website is deleted, the browser will delete the cookie file

Determine whether to log in for the first time

<?php//first determine if the COOKIE has the last login information if (!empty ($_cookie[' lastvisit ')) {  echo "the time you last logged in is". $_cookie[' Lastviat '];// Updated Setcookie ("lastvisit", "Data (y-m-d h:i:s)", Time () +3600); else{//User is the first time to login echo "First login";//Update Time Setcookie ("lastviait", "Data (" y-m-d h:i:s ")", timing () +3600);}? >

When you open the login screen, the user name and password are automatically filled in.

checklogin.php

Gets whether the user selected save Idif (!empty ($_post[' cookie)) {  Setcookie ("id", $id, Time ()-100);} else{  if (!empty ($_cookie[' id ')) {   Setcookie ("id", $id, Time ()-10);}}

Related Article

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.