PHP Cookie Working principle and example detailed _php skill

Source: Internet
Author: User
Tags setcookie

Cookies are something we use in PHP, but maybe a lot of friends don't know how cookies work, and here's a little bit to sort out the PHP cookie

Works with some examples of read-write operations for cookies.

Cookies and Session state

BS development, these two concepts are essential, first to a general understanding, no practical application is difficult to in-depth, in-depth look at the reference address!
What is a Cookie?

A Cookie is a small piece of textual information that accompanies the 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 that the Cookie contains. The basic work of cookies if a user accesses a page on a site again, when the user enters urlwww.*****.com, the browser looks for a cookie associated with the URL on the local hard disk. If the Cookie exists, the browser sends it to your site along with the page request.

What is the use of cookies?

The most fundamental use is that cookies can help the Web site save information about visitors. More generally, cookies are a way to keep Web applications continuous (that is, to perform state management). Make your Web site remember you.

• When a customer accesses a Web site based on PHP technology, you can use the Setcookie () function in PHP to generate a cookie, which is processed to send the cookie to the client and save it in the C:documents Andsettings User name Cookies directory.

Cookie is part of the HTTP header, so the Setcookie () function must be invoked before any content of the HTML itself is sent to the browser. This restriction is the same as the header () function (if you need to know the head () function, refer to it yourself).

• When the customer visits the website again, the browser will automatically send the C:documentsandsettings user name cookies directory and the corresponding cookies to the server, the server will be sent from the client's cookie automatically converted into a PHP variable. In PHP5, cookies sent by the client are converted to global variables. You can read it by $_cookie[' xxx '.

Define a cookie

• Set Cookies:

• Grammar: Boolsetcookie (stringname,[stringvalue,[int Expire,[stringpath,[stringdomain,[int Secure]]];

This cookie function can have 6 properties and is commonly used with 3 parameters.

1. Examples:

The code is as follows

$value = "The best way is by yourself";
Setcookie ("CookieName", $value, Time () +60*60*24*7);

1. Receive and process cookies

PHP has good support for cookies, and like form forms, PHP will automatically receive HTTP headers from the Web server and it. $_cookie["CookieName"] or $http_cookie_vars["CookieName" (not recommended) when receiving and form receiving

1. Note:

If the site has several different file directories and uses a cookie without a path, you can access the cookie only under the path where the file where the cookie is set is located. If the path is specified, the path as set is used as the specified path to access the cookie.

To create an array of cookies:

1. One:

The code is as follows

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

1. Secondly:

The code is as follows

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

Using arrays in 1.setcookie ()

The code is as follows

<?php
Setcookie ("Cookie[three]", "Cookiethree");
Setcookie ("Cookie[two]", "cookietwo");
Setcookie ("Cookie[one]", "Cookieone");
When the page is refreshed, it is displayed
if (isset ($_cookie[' cookie ')) {
foreach ($_cookie[' cookie ']as$name=> $value) {
echo "$name: $value <br/>n"
}
? >

Delete Cookies

1. To delete a cookie that already exists, there are two ways:

1, call Setcookie with only the name parameter, then the cookie named this name will be deleted from the client;
Setcookie ("MyCookie");//Delete MyCookie

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

For example:

The code is as follows

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

Delete MyCookie.

Note that 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 the Expiretime to 0 directly, or do not set this value.

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

Cookies considerations

• 1, Setcookie () can not have any HTML output, is a space, blank lines are not, must be in the HTML file content before the output set

• 2, Setcookie (), you call echo$_cookie["name" on the current page, there is no output. You must refresh or go to the next page to see the cookie value.

• 3. Do not use browser to treat cookies differently. The client can disable cookies, and the browser limits the number of cookies. A browser can create a maximum of 300 cookies, and each can not exceed 4KB, each Web site can set the total number of cookies cannot be more than 20.

• 4, cookies are stored in the client, the user has disabled cookies, your cookie is not a function of nature! Therefore, avoid relying too much on cookies, first think about the solution if the cookie is disabled in case.

The above is the information on the PHP cookie collation, there is a need for friends can refer to.

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.