Php cookie example

Source: Internet
Author: User
Tags php tutorial set cookie setcookie

1. Set cookie

In the php Tutorial, use 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 tutorial 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 a 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; the other is to set the cookie expiration time to time () or time () -1, 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 does not correctly handle cookies with domain names and paths, netscape communicator 4.05 and ms ie 3.0 does not correctly handle 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

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.