PHP Cookie learning notes and phpcookie learning notes

Source: Internet
Author: User

PHP Cookie learning notes and phpcookie learning notes

What is Cookie?

Cookie is a mechanism for storing data on a remote browser and tracking and identifying users. Simply put, Cookie is a text file temporarily stored on the user's hard disk by the Web server and subsequently read by the Web browser. When a user accesses the Web site again, the website reads the Cookies file to record the specific information of the visitor (such as the location of the last visit, the time spent, the user name and password ), to respond quickly, such as directly logging on to the website without entering the user ID and password.
The command format of a text file is as follows:

User name @ website address: Digital example .txt

Cookie Functions

Web servers can use Cookies to filter and regularly maintain information containing arbitrary information to determine the status during HTTP transmission. Cookies are commonly used in the following three aspects:
• Record some information of a visitor. For example, you can use cookies to record the number of times a user accesses a webpage or the information that a visitor has input. In addition, some websites can use cookies to automatically record the user name that the visitor logged on.
• Pass variables between pages. The browser does not save any variable information on the current page. When the page is closed, all the variable information on the page disappears. If you declare a variable id = 8 and want to pass the variable to another page, you can save the variable id as a Cookie, then, the value of the variable is obtained by reading the Cookie on the next page.
• Store the Internet pages viewed in the temporary Cookies folder to speed up future browsing.

Create Cookie

Use the setcookie () function in PHP to create a Cookie. The syntax format is as follows:

Copy codeThe Code is as follows: bool setcookie (string name [, string value [, int expire [, string path [, string domain [, int secure])

Read Cookie

In PHP, you can directly read the COOKIE value of the browser 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 timeRunning result:
This is the first time Cookie is saved.
Access time: 08:26:25-07-16

SecondRunning result:
Last access time: 16-07-16 08:26:25
Access time: 08:27:25-07-16

The above code first checks whether the Cookie file exists through the isset () function, and creates a Cookie file through the setcookie () function if the Cookie expires for 60 seconds.

Delete Cookie

When a Cookie is created and its expiration time is not set, the Cookie file is automatically deleted when the browser is closed. To delete a Cookie file before closing the browser, you can use either the setcookie () function to delete the file or manually delete the Cookie in the browser. The following is an introduction.

1. Use the setcookie () function to delete a Cookie

The method for deleting a Cookie is similar to that for creating a Cookie. The setcookie () function is also used to delete a Cookie. To delete a Cookie, you only need to set the second parameter in the setcookie () function to a null value, and set the expiration time of the Cookie of the 3rd parameter to a value smaller than the current time of the system.

For example, set the Cookie expiration time to the current time minus 1 second. The Code is as follows:

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

In the above Code, the time () function returns the current timestamp in seconds. After the expiration time is reduced by 1 second, the past time is obtained, thus deleting the Cookie.

2. manually delete the Cookie in the browser

When using Cookies, Cookies automatically generate a text file stored in the temporary Cookies folder of IE browser. It is very convenient to delete Cookie files in a browser.

Cookie Lifecycle

If no time is set for a Cookie, it indicates that its life cycle is the period of browser session. If you close ie, the Cookie will automatically disappear. This Cookie is called a session Cookie. It is generally stored in the memory instead of on the hard disk.

If the expiration time is set, the browser will save the Cookie to the hard disk, and the Cookie will remain valid when you open ie again until its validity period times out.

Although cookies can be stored in Client browsers for a long time, they are not static. The browser can store up to 300 Cookie files, and each Cookie file supports a maximum capacity of 4 kb. Each domain name supports up to 20 cookies. If the limit is reached, the browser automatically deletes Cookies randomly.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.