How to set, use, and delete cookies in PHP _ PHP Tutorial

Source: Internet
Author: User
Tags set cookie
How to set, use, and delete cookies in PHP. 1. set CookiePHP to use the SetCookie function to set cookies. Note that Cookie is part of the HTTP header and is used to transmit information between the browser and the server. therefore

1. set Cookie

PHP uses the SetCookie function to set the Cookie. Note that Cookie is a part of the HTTP header used to transmit information between the browser and the server. Therefore, the Cookie function must be called before any HTML file is output.

The SetCookie function defines a Cookie and attaches it to the end of the HTTP header. the prototype of the SetCookie function is as follows:

Int SetCookie (string name, string value, int expire, string path, string domain, int secure );

All parameters except name are optional. The value, path, and domain parameters can be replaced by an empty string, indicating that they are not set. The expire and secure parameters are numeric and can be expressed as 0. The expire parameter is a standard Unix time mark, which can be obtained using the time () or mktime () function, in seconds. The secure parameter indicates whether the Cookie is transmitted over the network through the encrypted HTTPS protocol. The Cookie currently set does not take effect immediately, but will not be visible until the next page. this is because the Cookie is transmitted from the server to the client's browser on the configured page, and the Cookie can be retrieved from the client's machine and sent back to the server on the next page. Setting a Cookie on the same page actually goes from the back to the back. if you want to delete a Cookie before inserting it, you must first write the insert statement and then write the delete statement, otherwise, unexpected results may appear. Here are several examples:

Simple:

SetCookie ("MyCookie", "Value of MyCookie ");

With expiration time:

SetCookie ("WithExpire", "Expire in 1 hour", time () + 3600); // 3600 seconds = 1 hour

Everything:

SetCookie ("FullCookie", "Full cookie value", time () + 3600, "/forum", ".phpuser.com", 1 );

Here is another note. for example, if your site has several different directories, if you only use cookies without paths, the Cookie set on the page under one directory is invisible on the page of another directory, that is, the Cookie is path-oriented. In fact, even if no path is specified, the WEB server will automatically pass the current path to the browser, and the specified path will force the server to use the set path. To solve this problem, add the path and domain name when calling SetCookie. the domain name format can be "www.phpuser.com" or ".phpuser.com ". The SetCookie function indicates the value part, which is automatically encoded during transmission. that is to say, if the value of "test value" is changed to "test % 20value" during transmission ", same as the URL method. Of course, this is transparent to programs, because PHP will automatically decode the Cookie value when receiving it.

To set multiple cookies with the same name, use an array:

SetCookie ("CookieArray []", "Value 1 ");

SetCookie ("CookieArray []", "Value 2 ");

Or

SetCookie ("CookieArray [0]", "Value 1 ");

SetCookie ("CookieArray [1]", "Value 2 ");

2. receive and process cookies

PHP supports Cookie receiving and processing very well and is completely automatic. it is as simple as the FORM variable principle. For example, if you set a Cookie named MyCookier, PHP will automatically analyze it from the HTTP header received by the WEB server and form a variable named $ myCookie, which is the same as a common variable, the value of this variable is the Cookie value. Arrays also apply.

Another method is to reference the global variable $ HTTP_COOKIE_VARS array of PHP.

Examples are as follows: (assuming these are all set in the previous page and still valid)

Echo $ MyCookie;

Echo $ CookieArray [0];

Echo count ($ CookieArray );

Echo $ HTTP_COOKIE_VARS ["MyCookie"];

That's simple.

3. delete a Cookie

There are two ways to delete an existing Cookie:

One is to call the SetCookie with only the name parameter, the Cookie named this name will be deleted from the relationship host;

Another method is to set the Cookie's expiration time to time () or time ()-1. then, the Cookie is deleted after the page is viewed (in fact, it is invalid ). Note that when a Cookie is deleted, its value is still valid on the current page.

4. Cookie usage restrictions

First, it must be set before HTML file content output;

Second, different browsers may not provide speeches on Cookie processing, and sometimes error results may occur. For example, ms ie + service pack 1 cannot correctly process cookies with domain names and paths. Netscape Communicator 4.05 and ms ie 3.0 cannot correctly process cookies without path and time. Ms ie 5 does not seem to be able to process cookies with domain names, paths, and time. This is what I found when designing the website page.

The third limitation is on the client. A browser can create a maximum of 30 cookies, each of which cannot exceed 4 kB. each WEB site can set a maximum of 20 cookies.

I will talk about Cookie here.

PHP uses the SetCookie function to set the Cookie. Note that Cookie is part of the HTTP header and is used to transmit information between the browser and the server. Therefore, it must be in...

Related Article

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.