Learn PHP cookie processing function _php instance

Source: Internet
Author: User
Let's take a look at 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:
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):

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:

The reason here is to refresh the page is because the value of the cookie will not be stored in the C Ookie variable immediately after the call to Setcookie (), but the value of the cookie will appear in the cookie variable as the HTTP request is sent to the server. Instead, the value of the cookie appears in _cookie as the HTTP request is sent to the server. (Blog Park for dollar characters do not support ...) Ah ah ah)

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):

Setcookie ("Cookie_one", "A", Time () +60*60);    The cookie expires after one hour Setcookie ("Cookie_two", "B", Time () +60*60*24);  Cookies expire after one day Setcookie ("Cookie_three", "C", Mktime (23,53,19,10,09,2020));  Cookies expire on October 9, 2020 23:53 19 seconds Setcookie ("Cookie_four", "D");   Cookies expire after you close the browser  

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:
Setcookie (' Test ', 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: 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.

The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support the script home.

  • 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.