Cookies and their use (ii)

Source: Internet
Author: User
Tags array header set cookie setcookie variable valid domain domain name
Part of the cookie tells about the technical background of the cookie, which explains how to set up, use, delete cookies, and some of the limitations of cookies in PHP. PHP support for cookies is transparent and easy to use.





1, set cookie





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 current set of cookies 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 in the Settings page, and the next page browser can take the cookie out of the client's machine and send it back to the server.


set cookies on the same page, actually 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.


to see a few examples:


Simple:


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


with failure time:


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


have 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 cookie, in a directory of the page set in a cookie 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 using the following method:


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, which 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 these are all set in the previous page and still valid)


Echo $MyCookie;


echo $CookieArray [0];


echo Count ($CookieArray);


echo $HTTP _cookie_vars["MyCookie"];


is so simple.





3, delete cookie





to delete a cookie that already exists, there are two ways:


one is to invoke a Setcookie with only the name parameter, then 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 the page has been browsed (in fact, it is invalid).


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





4, restrictions on using cookies





must first 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 was designing 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.








on the topic of cookies, here we are.





(since the cookie was originally defined by Netscape, it is accompanied by Netscape's official original definition URL for cookies: http://www.netscape.com/newsref/std/cookie_spec.html)








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.