PHP Cookie Processing function

Source: Internet
Author: User
Tags setcookie

(O゜▽゜) o☆[bingo!]

OK, let's see what cookies are.

A cookie is a small file that the server leaves on the client to identify the user or store some data (note that the session is stored on the server side, which is one of the differences). We usually log in to a portal, there will be the option "Save Login" or "Next automatic login", when we tick, the server will create a cookie file in our browser to save our information. Whenever a computer requests a page through a browser, it sends a cookie at the same time. With PHP, you can create and retrieve the value of a cookie. Cookies are a very important role in the web, and cookies are created as early as in Netscape's browser. Cookies are often used for user authentication systems.

1. Create a cookie

The function Setcookie () can generate cookies in PHP. Since the cookie is the content of the HTTP header section, you must call Setcookie () before outputting any data, which is similar to the header (), and defines:

1 bool setcookie( string name[,string value][,int expire][,string path][,string domain][,bool secure][,bool HttpOnly])

A lot of parameters Ah! Σ (⊙▽⊙ "A is not urgent, let's look at the role of each parameter:

Name: Required, indicates the name of the cookie.

Value: Optional, represents the cookie value, stored in the client, when empty, means to revoke the information of the cookie in the client (so that the cookie can be deleted).

Expire: Optional, indicates the effective cutoff time of the cookie, which is the expiration time, if not specified or specified as 0, is usually invalidated when the browser is closed.

Path: optional, cookie valid path.

Domain: Optional, cookie valid domain name.

Secure: Indicates that the secure transport of HTTPS is valid.

Instance (create a cookie named Test with the value of China):

1 <? PHP 2 Setcookie ("Test", "China"); 3 ?>

The value given by using Setcookie () can only be a number or a string , not another complex structure.

2. Obtaining cookies

Once you have created a cookie, you can use the predefined variable $_cookie to get the cookie. However, you can only get cookies on other pages, because in PHP, the cookie that is set does not take effect on this page unless the page is refreshed .

Instance:

1 <? PHP 2 Setcookie ("Test", "China"); 3 Echo "Cookie is". $_cookie ["Test"]; 4?>

The reason here is to refresh the page is because the value of the cookie is not stored in the $_cookie variable immediately after the call to Setcookie (), but the value of the cookie appears in $_cookie when the HTTP request is sent to the server.

Expiration date of 3.cookie

A cookie has a life cycle, which is the effective time that a cookie exists. You can set the third parameter to set a valid time.

Instance (several ways to set a cookie's validity time):

 1  setcookie  ("Cookie_one", "A", Span style= "color: #008080;"       >time  () +60*60); // cookie expires in one hour  2  setcookie  ("Cookie_two", "B", time  () +60*60*24); // cookie expires after one day  3  setcookie  ("Cookie_three", "C", mktime  (23,53,19,10,09,2020)); // cookie expires on October 9, 2020 23:53 19 seconds      4  setcookie  ("Cookie_four", "D"); //  

Valid path for 4.cookie

The path in the cookie is used to control which path the cookie is set to be valid, and the default is '/', which is valid under all paths, that is, the entire server domain name, and when the other path is set, it is only valid under the set path and sub-path, for example:

1 Setcookie  Time (), 0, '/path ');

The above settings will make test valid under/path and sub-path/PATH/ABC, but the cookie value of test cannot be read in the root directory.

In general, most of the use of all paths, only in a very small number of special needs, will set the path, in this case only in the specified path to pass the cookie value, can save data transmission, enhance security and improve performance.

5. Delete Cookies

Deleting a cookie is simpler and is done by Setcookie () (Do not use unset ()!!! The following code is a simple example:

1 Setcookie ("Test", "");

The purpose of deleting a cookie is to set the second parameter to null . If you set a cookie with a specific value for the cookie, you still need to provide these parameters when you delete the cookie so that PHP can delete the cookie correctly.

PHP Cookie Processing function

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.