Php cookie study notes

Source: Internet
Author: User
Tags current time php script set cookie setcookie website server

What does Cookie mean?

Cookie is used to store the user name, password, and number of visits to the site. When a website is accessed, the Cookie sends the html webpage to a small segment of information in the browser and saves it as a script on the client's computer.

What is the use of cookies? If a server is used to record the number of times that a user has logged on to the site, the accumulated data for a long period of time will inevitably be very huge, and the load on the server will be very high. Therefore, you can store data in your own computer, and when necessary, the server will read the Cookie on your computer to extract data, so that the server does not need to record a large amount of data.


Generally, cookies return to the browser from the server through HTTP Headers. First, the server uses the Set Cookie Header in the response to create a Cookie. The browser then uses the Cookie Header in the request to include the created Cookie and return it to the server to complete browser authentication.


Cookie Creation

Php needs to call the setcookie () function to create a Cookie. Its structure is as follows:

Bool setcookie (string name [, string value [, int expiration [, string path [, bool secure])
Parameter name indicates the Cookie name. Other parameters are optional;
The parameter value is the value stored in the Cookie;
The expiration parameter specifies the Cookie expiration time;
The parameter path specifies the valid path of the Cookie on the server, which is used to set the folder where the Cookie will be sent to the server;
The secure parameter specifies whether the Cookie is sent through a secure https link.
Php cookie instance code:

The code is as follows: Copy code

<? Php
Setcookie ("c1", "my name is Rose", time () + 3600 );
/* Create a cookie named "c1" with the expiration time of 3600 seconds */
?>

Cookie access

Most variables only occupy space in the memory. Therefore, when the php script ends, the variables are also released and cleared from the memory. Cookie can store the value of the variable to the hard disk of the user's computer for a long time. When we need to call the value of this variable, read the name of the Cookie.

The Cookie created in the previous section has already created a Cookie named "c1 & Prime;". Let's test whether the Cookie is successfully created.

Php Cookie access instance:

The code is as follows: Copy code

<? Php
Echo "the Cookie value of c1 is". $ _ COOKIE ['C1']. "<br> ";
Echo "the Cookie value of c2 is". $ _ COOKIE ['C2'];
?>

Cookie Time

If the Cookie validity period is not set, the Cookie disappears when the browser is closed. To retain a Cookie, you must set a valid time for it.

Php Cookie time setting method:

Time () + seconds
Time () indicates the current time of the user's operating system, followed by the number of seconds is the Cookie effective time. To create a specific date, you can use the mktime () function. Its structure is as follows:

Mktime (hour, minute, second, month, day, year)
If you want to delete a Cookie immediately, you can set the time to the past time.

Php Cookie time setting example:

The code is as follows: Copy code
Setcookie ("a", "10", time ()-60);/* Set the time to 60 seconds before the value is deleted immediately */
Setcookie ("a", "10", time () + 60);/* Set the effective time to 60 seconds */
Setcookie ("a", "10", mktime (,);/* the expiration time is November 1, 2011 00:00:00 */

The following describes a simple statistical program designed using cookies:

The code is as follows: Copy code

<? Php
$ Count = $ _ COOKIE ['user'];
$ Count ++;
Setcookie ("user", $ count, time () + 3600 );
Echo "this is your $ count access! ";
?>

Cookie array

We can also create a php cookie array. The following describes the implementation method through an instance.

Code:

The code is as follows: Copy code
<? Php
Setcookie ('name [1] ', 'rose ');
Setcookie ('name [2] ', 'John ');
Setcookie ('name [3] ', 'mikle ');
If (is_array ($ _ COOKIE ['name']) {/* determine whether an array is used */
Foreach ($ _ COOKIE ['name'] as $ name => $ value ){
Echo "$ name: $ value <br> ";
  }
 }
Else echo "not an array ";
?>


Cookie restrictions

Cookies are used to store important information that users browse websites. To prevent information leakage caused by misuse, the following restrictions apply to cookies:

The browser records the user's cookie size limit within 4 kb;
The browser can only store a website server with a maximum of 20 cookies. If the number is exceeded, the previously saved cookies will be deleted.
Each user's browser can access a maximum of 300 cookies.
Users can set whether to enable Cookie storage information in the browser. Therefore, to use Cookie storage information, you must first confirm whether the Cookie function in the browser has been enabled. For example, in the IE 9 browser, open the "properties"-"privacy"-"advanced" option and Select Enable:


Tip: after the browser creates a Cookie, each request for the website will carry the Cookie in the Header. In addition, the browser keeps sending messages and knows where the Cookie expires. However, for requests from other websites, cookies will never be sent.

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.