Tutorial on cookie variable usage in PHP

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags browser browsers client content cookie cookies delete deleted

PHP cookies using a detailed tutorial
1. Set Cookies

PHP uses the Setcookie function to set cookies. It is important to note that cookies are part of the HTTP protocol header and are used to pass information between browsers and servers, so you must call the cookie function before any content output that belongs to the HTML file itself.

The Setcookie function defines a cookie and attaches it to the back of the HTTP header, and 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. Value,path,domain three parameters can be substituted with an empty string, indicating that there is no setting; expire and secure two parameters are numeric and can be expressed in 0. The expire parameter is a standard UNIX time token, which can be obtained in seconds () or mktime (). The secure parameter indicates whether this cookie is transmitted over the network via an encrypted HTTPS protocol.

The cookie that is currently set does not take effect immediately, but is not visible until the next page. This is because cookies are delivered to the client's browser by the server on this page of the setting, and the next page browser can take the cookie out of the client's machine and send it back to the server.

Setting cookies on the same page is actually coming from the back, so if you want to delete one before inserting a new cookie, you have to write the inserted statement before you write the deleted statement, or you may have an expected result.

Take a look at a few examples:

Simple:

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

With Expiration Time:

Setcookie ("Withexpire", "Expire in 1 Hour", Time () +3600);//3600 seconds = 1 hours

Everything:

Setcookie ("Fullcookie", "full cookie value", Time () +3600, "/forum", ". phpuser.com", 1);

Here are a few things to explain, such as your site has several different directories, then, if only with no path of the cookie, the page in a directory of the cookie set in another directory page is not visible, that is, cookies are path-oriented. In fact, even if no path is specified, the Web server automatically passes the current path to the browser, and the specified path forces the server to use the path of the setting. The solution to this problem is to add the path and domain name when calling Setcookie, the format of the domain name can be "www.phpuser.com", but also ". phpuser.com".

The part of the Setcookie function that represents value is automatically encode when it is passed, that is, if value "test value" becomes "Test%20value" at the time of delivery, as is the case with the URL. Of course, this is transparent to the program because it is automatically decode when PHP receives the value of the cookie.

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

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's support for cookies receiving and processing is very good, and is completely automatic, as is the principle of form variables, particularly simple.

For example, setting up a cookie,php named Mycookier will automatically parse it out of the HTTP header received by the Web server and form a variable like a normal variable, named $ MyCookie, whose value is the value of the cookie. Arrays are also applicable. Another option is to refer to the PHP global variable $http_cookie_vars array.

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

Echo $MyCookie;

echo $CookieArray [0];

echo count ($CookieArray);

echo $HTTP _cookie_vars["MyCookie"];

It's as simple as that.

3, delete cookies

There are two ways to delete an existing cookie:

The first is to invoke a Setcookie with only the name parameter, and the cookie named this name will be deleted from the connections machine; Another way is to set the expiration time for the cookie to be either times () or ()- 1, the cookie is deleted after browsing the page (in fact, it is invalid).

Note that when a cookie is deleted, its value is still valid on the current page.

4. Limitations of using cookies

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

Second, different browsers disagree with the processing of cookies, and sometimes the wrong results are found. For example: Ms Ie+service PACK 1 does not correctly handle Cookie,netscape Communicator 4.05 and Ms IE 3.0 with domain names and paths, and does not properly handle cookies without paths and time. As for Ms IE 5, it seems unable to handle cookies with domain names, paths, and time. This is what I found when I designed the page of this website.

The third limit is on the client. A browser can create a maximum of 30 cookies, and each can not exceed 4KB, each Web site can set the total number of cookies cannot be more than 20.

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.