PHP Cookies Learn notes _php tips

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

What is a cookie

A cookie is a mechanism for storing data on a remote browser side to track and identify the user. 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 a Web browser. When a user visits a Web site again, the site can quickly respond by reading a cookie file that records the visitor's specific information (such as the location of the last visit, the time spent, the username and password), etc.
The command format for a text file is as follows:

User name @ web address [digital].txt

The function of cookies

The Web server can filter and regularly maintain this information by using cookies that contain the arbitrariness of the information to determine the state of the HTTP transmission. Cookies are commonly used in the following 3 areas:
• Record certain information about a visitor. If you can use cookies to record the number of times a user accesses a Web page, or to record the information that a visitor has entered, in addition, some Web sites can use cookies to automatically record the user's last logon name.
• Pass variables 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.
• Store the viewed Internet pages in a cookie Temp folder, which can improve the speed of browsing later.

Creating cookies

Create cookies in PHP using the Setcookie () function. The syntax format is as follows:

Copy Code code as follows:
BOOL Setcookie (String name[,string value[,int expire[, String path[,string domain[,int secure]]]

Read cookies

In PHP, you can read the COOKIE value from the browser side directly through 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 ">
 
 

First Run results:
This is the first time you save cookies
This visit time: 16-07-16 08:26:25

second run result:
Last access time: 16-07-16 08:26:25
This visit time: 16-07-16 08:27:25

The above code, first through the isset () function to detect the existence of a cookie file, does not exist through the 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, if its expiration time is not set. If you want to delete a cookie file before you close the browser, there are two ways: one is to delete using the Setcookie () function, and the other is to manually delete cookies in the browser. The following are described separately.

1. Use the Setcookie () function to delete cookies

The way to delete cookies and create cookies is basically similar, and the Setcookie () function is used to delete cookies. Deleting a cookie simply sets the second argument in the Setcookie () function to a null value, setting the expiration time of the 3rd parameter cookie to less than the current time of the system.

For example, the expiration time of the cookie is set to the current time minus 1 seconds, and the code is as follows:

Setcookie ("name", "", Time ()-1);

In the above code, the time () function returns the current timestamp in seconds, and the expiration time by minus 1 seconds will result in the past time, thereby removing the cookie.

2. Manually delete cookies in the browser

When using cookies, cookies automatically generate a text file stored in the Internet Explorer's Cookies temporary folder. Deleting a cookie file in a browser is a convenient way to do so.

The life cycle of cookies

If the cookie does not set a time, it means that its lifecycle is the duration of the browser session, and as long as the IE is turned off, the cookie automatically disappears. This cookie is called a session cookie and is generally not stored on the hard disk, but in memory.

If an expiration time is set, the browser saves the cookie to the hard disk and remains in effect when it is opened again, until its validity expires.

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

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.