PHP cookie usage tutorial

Source: Internet
Author: User

1. Set cookie

PHP uses the setcookie function to set the cookie. Note that cookie is part of the HTTP header.
Used to transmit information between the browser and the server.
Called before output of any HTML file content
Cookie function.

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. expire and
The two secure parameters are numeric and can be expressed as 0.Expire

The parameter is a standard UNIX time mark.
Can be obtained using the time () or mktime () function, in seconds
Unit.

Secure
Indicates whether the cookie is encrypted.
HTTPS
The Protocol is transmitted over the network.

 

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 );

 

For example, if your site has several different directories, if you only use cookies without paths
Directory pages are invisible, that is, cookies are path-oriented. Actually
Even if no path is specified, the web server will automatically pass the current path to the browser.

The server uses the specified path. To solve this problem, add the path and domain name when calling setcookie. The domain name format can be "www.phpuser.com",
".


Phpuser.com ".

The part of the setcookie function that represents the value is automatically encoded when it is passed.
That is to say, if the value is "Test
"Value" is changed to "test % 20value" during transmission, which is the same as the URL method. Of course, this is transparent to the program, because when PHP receives the cookie value
Decode it automatically.

 

If you want to set multiple cookies with the same name
To use an array, the method is:

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
Mycookier cookie. php automatically analyzes the cookie from the HTTP header received by the Web server and forms a variable named $
Mycookie. 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:

 

First, the call only includes the name parameter.
Then, the cookie named "this name" will be deleted from the linked host;

Another way 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. The cookie with the domain name and path cannot be correctly processed. Netscape Communicator 4.05 and Ms IE
3.0 the cookie with no path or time cannot be correctly processed. For Ms ie 5
It seems that the cookie with domain name, path, and time cannot be processed. This is what I found when designing the website page.

The third limitation is on the client.

 

One browser can
The maximum number of created cookies is 30, and each
Cannot exceed 4 kb
, Each web site
The total number of cookies that can be set cannot exceed 20
.

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.