PHP Learning Note-cookie

Source: Internet
Author: User
Tags php language set cookie setcookie

Reprint please indicate the source:
http://blog.csdn.net/hai_qing_xu_kong/article/details/51922536
This article is from: "Gu Linhai's Blog"

Objective

It's been one or two weeks since blogging, and it's been a busy time to get familiar with the project in a new company that's ready to start in some channels, so it takes a lot of time to fix bugs and add features. Well, continue with the topic of this chapter.

Cookie Management


What is a cookie

A cookie is a mechanism for storing data on a remote browser to track and identify users. Simply put, a cookie is a text file that the Web server temporarily stores on the user's hard disk and is then read by the Web browser. When the user visits the Web site again, the website will respond quickly by reading the cookie file to record the visitor's specific information (such as the location of the last visit, time spent, user name and password, etc.), for example, if you do not need to enter the user's ID and password on the page to login directly to the website.
The command format for the text file is as follows:

用户名@网站地址[数字].txt



Features of cookies

The Web server can apply cookies to contain information about the arbitrariness to filter and regularly maintain this information in order to determine the status in the HTTP transmission. Cookies are commonly used in the following 3 areas:

    • Record certain information about a visitor. If a cookie can be used to record the number of times a user visits a webpage, or to record information that has been entered by a visitor, some websites may use cookies to automatically record the user's last login name.
    • Pass a variable between pages. The browser does not save any variable information on the current page, and all variable information on the page disappears when the page is closed. If the user declares a variable id=8, to pass the variable to another page, you can save the variable ID as a cookie and then fetch the value of the variable by reading the cookie on the next page.
    • Storing the Internet pages you view in the cookie Temp folder can increase the speed of your browsing later.


Create a cookie

Create a cookie in PHP with the Setcookie () function. The syntax format is as follows:

bool setcookie(string name[,stringvalue[,intstring path[,string domain[,int secure]]]]])
Parameters Description Example
Name Variable name of the cookie You can call a COOKIE with the variable named CookieName by $_cookie["CookieName")
Value The value of the cookie variable, which is stored on the client and cannot be used to hold sensitive data A value named values can be obtained by $_cookie["values"
Expire The expiration time of the cookie, expire is the standard UNIX time tag, which can be obtained in seconds () or the Mktime () function. If you do not set a cookie's expiration time, the cookie will always work, unless you manually delete it
Path Valid path of cookie on server side If the parameter is set to "/", it is valid throughout domain and, if set to "/11", it is valid in the/11 directory and subdirectories under domain. The default is the current directory
Domain A valid domain name for cookies If you want the cookie to be valid for all subdomains under the hhh.com domain name, it should be set to hhh.com
Secure Indicates whether the cookie is only secured by HTTPS with a value of 0 or 1 If the value is 1, the cookie is valid only on HTTPS connections, and if the value is the default value of 0, the cookie is valid on both HTTP and HTTPS connections
Read cookies

The COOKIE value of the browser can be read directly in PHP via the Super Global Array $_cookie[].

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "><html xmlns="http://www.w3.org/1999/xhtml"><head>    <meta http-equiv="Content-type" Content="text/html; charset=gb2312 " />    <title>PHP Language Basics</title></head><body><?phpDate_default_timezone_set ("PRC"); Header ("content-type:text/html; charset=gb2312 ");if(!isset($_cookie["Visittime"]) {Setcookie ("Visittime", Date ("y-m-d h:i:s"));Echo "This is the first time a cookie is saved"."<br>";}Else{Setcookie ("Visittime", Date ("y-m-d h:i:s"), time () + -);Echo "Last Access Time is:".$_cookie["Visittime"];Echo "<br>";}Echo "Visit time:". Date"y-m-d h:i:s");?></body></html>

First Run Result:
This is the first time a cookie is saved
Visit Time: 16-07-16 08:26:25

Second run Result:
Last access time: 16-07-16 08:26:25
Visit Time: 16-07-16 08:27:25

The above code, first through the isset () function to detect the existence of a cookie file, there is no Setcookie () function to create a cookie file, if there is a set cookie expiration time of 60 seconds.

Delete Cookies

When a cookie is created, its cookie file is automatically deleted when the browser is closed without setting its expiration time. If you want to delete a cookie file before closing the browser, there are two ways: one is to use the Setcookie () function and the other is to delete the cookie manually in the browser. The following are described separately.

1. Use the Setcookie () function to delete cookies

deleting cookies and creating cookies is basically similar, and deleting cookies also uses the Setcookie () function. To delete a cookie simply set the second parameter in the Setcookie () function to a null value and set the expiration time of the 3rd parameter cookie to less than the current time of the system.

For example, set the expiration time of the cookie to the current time minus 1 seconds, as shown in the following code:

setcookie("name", "", time()-1);

In the preceding code, the time () function returns the current timestamp in seconds, and the expiration time is reduced by 1 seconds to get past the period, thereby removing the cookie.

2. Manually delete cookies in the browser
When using cookies, cookies automatically generate a text file that is stored in the Cookies Temp folder in Internet Explorer. Deleting a cookie file in a browser is a very convenient way.

The life cycle of a cookie

If the cookie does not set the time, it means that its lifetime is the period of the browser session, and the cookie disappears automatically as soon as the Internet Explorer is closed. This cookie, known as a session cookie, is generally not saved on the hard disk, but in memory.

If the expiration time is set, then the browser will save the cookie to the hard disk, and the Internet Explorer will remain in effect until it expires.

Although cookies can be kept in the client browser for a long time, they are not immutable. Because the browser allows a maximum of 300 cookie files, and each cookie file supports a maximum capacity of 4KB, each domain name supports up to 20 cookies, and if the limit is reached, the browser automatically deletes the cookies randomly.

PHP Learning Note-cookie

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.