PHP cookie working principle and cookie Instance detailed _php tutorial

Source: Internet
Author: User
Tags setcookie
Cookies are commonly used in PHP, but many friends do not know how the cookie works, the following small part to you to tidy up about the PHP cookie working principle and some of the cookie read and write operation instance.


Cookie and Session Status

To do BS development, these two concepts are essential, first to a general understanding, no practical application difficult to go deep, look at the reference address!
What is a Cookie?
A Cookie is a small piece of textual information that accompanies a user request and the page is passed between the WEB server and the browser. Each time a user accesses a site, the WEB application can read the information contained in the Cookie. How a cookie works if a user accesses a page on the site again, when the user enters urlwww.*****.com, the browser looks for the Cookie associated with the URL on the local hard drive. If the Cookie exists, the browser sends it to your site along with the page request.

What are the uses of cookies?
The most fundamental use is that cookies can help a Web site keep information about visitors. More generally, cookies are a way to maintain Web application continuity (that is, to perform "state management"). Make the Web site remember you.

• When a customer accesses a Web site based on a PHP technology, a cookie can be generated in PHP using the Setcookie () function, which is processed to send the cookie to the client and stored in the C:documents Andsettings user name in the cookie directory.
• Cookies is part of the HTTP header, so the Setcookie () function must be called before any content in the HTML itself is sent to the browser. This restriction is the same as the header () function (see the head () function for your own reference).
• When the customer visits the website again, the browser automatically sends the cookie in the C:documentsandsettings username and cookies to the server, and the server will automatically convert the cookie from the client to a PHP variable. In PHP5, the cookie sent by the client is converted to a global variable. You can read it by $_cookie[' xxx '.
Define a cookie
• Set Cookies:
• Syntax: Boolsetcookie (stringname,[stringvalue,[int Expire,[stringpath,[stringdomain,[int Secure]]]);
This cookie function can have 6 properties, which are commonly used with 3 parameters.

1. Example:

The code is as follows Copy Code

$value = "The best-of-the-yourself";

Setcookie ("CookieName", $value, Time () +60*60*24*7);

1. Receiving and processing cookies
PHP has good support for cookies, like form forms, when it receives HTTP headers automatically from the Web server and it. When receiving the same as the form receive, use $_cookie["CookieName"] or $http_cookie_vars["CookieName"] (not advocated)

1. Note:
If a Web site has several different file directories, using a cookie without a path, the cookie can only be accessed under the path of the file where the cookie is set. If you specify a path, follow the path you set as the specified path to access the cookie.

To create an array of cookies:
1. Firstly:

The code is as follows Copy Code

Setcookie ("cookiearray[0]", "Value1");

Setcookie ("cookiearray[1]", "Value2");

1. Secondly:

The code is as follows Copy Code

Setcookie ("cookiearray[' One ']", "Value1");

Setcookie ("cookiearray['", "Value2");

Using arrays in 1.setcookie ()

The code is as follows Copy Code

Setcookie ("Cookie[three]", "Cookiethree");

Setcookie ("Cookie[two]", "cookietwo");

Setcookie ("Cookie[one]", "Cookieone");

After the page is refreshed, it appears

if (Isset ($_cookie[' COOKIE ")) {

foreach ($_cookie[' COOKIE ']as$name=> $value) {

echo "$name: $value
n ";

}

}

?>

Delete Cookies
1. To delete an existing cookie, there are two options:
1. Call the Setcookie with the name parameter only, then the cookie named this name will be deleted from the client;

Setcookie ("MyCookie");//Delete MyCookie

2, the period of time on the line, then this cookie on this page after browsing has been deleted (in fact, it is invalid).

For example:

The code is as follows Copy Code

Setcookie ("MyCookie", "Value", Time ()-1);

Delete MyCookie.

Note: When a cookie is deleted, its value is still valid on the current page. If you want to set the cookie to expire after the browser is closed. You can set Expiretime to 0 directly or not.

For example: Setcookie ("name", "value", 0).

Cookie Precautions
• 1, Setcookie () can not have any HTML output, is a space, blank lines are not, must be in the HTML file content output before setting
• After 2, Setcookie (), you will not have output on the current page called echo$_cookie["name". You must refresh or go to the next page to see the cookie value.
• 3. Do not use the browser to treat different cookies. The client can disable cookies and the browser will limit the number of cookies. A browser can create a maximum of 300 cookies, and each cannot exceed 4KB, and each Web site can set a total of no more than 20 cookies.
• 4, cookies are stored on the client, the user has disabled the cookie, and your cookie naturally does not work! Therefore, to avoid over-reliance on cookies, be sure to consider the solution if the cookie is disabled in case.

http://www.bkjia.com/PHPjc/633059.html www.bkjia.com true http://www.bkjia.com/PHPjc/633059.html techarticle in PHP, cookies are commonly used, but many friends do not know how the cookie works, the following small series to everyone to organize a bit about the PHP cookie working principle and some for ...

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