How PHP cookies work and examples

Source: Internet
Author: User
Tags setcookie
This article mainly introduces the use of PHP cookies, PHP is often used in the development process of the cookie, here is a code example, in order to understand, the need for small partners to see

Cookies are commonly used in PHP, but many friends don't know how cookies work, so let's make a little bit of a cookie about PHP.

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

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

$value = "the best 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

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

1. Secondly:

The code is as follows

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

Using arrays in 1.setcookie ()

The code is as follows

<?phpsetcookie ("Cookie[three]", "Cookiethree"), Setcookie ("Cookie[two", "Cookietwo"), Setcookie ("Cookie[one"), "Cookieone")//Refresh the page, display if (Isset ($_cookie[' cookie ')) {foreach ($_cookie[' cookie ']as$name=> $value) {echo "$ Name: $value <br/>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

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.

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.