PHP operation Cookie-related tips sharing _php tutorial

Source: Internet
Author: User
Tags set cookie setcookie

PHP operation Cookie1, setting cookies

PHP uses the Setcookie function to set cookies. It is important to note that a cookie is a part of the HTTP protocol header that is used to pass information between the browser and the server, so the cookie function must be called before any content that is part of the HTML file itself is exported.

The Setcookie function defines a cookie and attaches it behind the HTTP header, and the Setcookie function is prototyped 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 represented by 0. The expire parameter is a standard UNIX time stamp, which can be obtained in seconds using either the Times () or the Mktime () function. 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 due to the fact that the cookie is passed to the client's browser on this page and the next page browser will be able to remove the cookie from the client's machine and return it to the server.

Setting a cookie on the same page is actually going backwards, so if you want to delete one before inserting a new cookie, you have to write the inserted statement and then write the deleted statement, otherwise you may have unwanted results.

Take a look at some examples:

Simple:

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

With time of failure:

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

There are everything:

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

Here is another point to explain, such as your site has a few different directories, if you use only the cookie without path, the page in one directory is not visible on the page of another directory, that is, the cookie is path-oriented. In fact, even if the path is not 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 passed, that is, if value is "test value" when it is passed, it becomes "Test%20value", as is the method of the URL. Of course, this is transparent to the program, because PHP automatically decode the value of the cookie when it receives it.
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");

PHP operation Cookie2, receiving and processing cookies

PHP's support for the reception and processing of cookies is very good, is completely automatic, as is the principle of the form variable, especially simple.

For example, setting up a cookie,php named Mycookier automatically parses it from the HTTP header received by the Web server and forms a variable that is the same as a normal variable, named $myCookie, whose value is the value of the cookie. arrays also apply. Another way is to reference the PHP global variable $http_cookie_vars array.

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

Echo $MyCookie;
echo $CookieArray [0];
echo count ($CookieArray);
echo $HTTP _cookie_vars["MyCookie"];
It's so simple.

PHP operation Cookie3, delete cookies

There are two ways to delete an already existing cookie:

One is to call the Setcookie with the name parameter only, then the cookie named this name will be deleted from the connections machine, and the other option is to set the cookie to expire at time () 1, then this 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.

Limitations of PHP operation Cookie4, use of cookies

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

Second, different browsers are inconsistent with the processing of cookies and sometimes have incorrect results. 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 correctly handle cookies without path and time. As for MS IE 5, it is not possible to process cookies with domain names, paths, and times. 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 cannot exceed 4KB, and each Web site can set a total of no more than 20 cookies.

On the topic of PHP operating cookies, here it is.


http://www.bkjia.com/PHPjc/445949.html www.bkjia.com true http://www.bkjia.com/PHPjc/445949.html techarticle PHP operation Cookie1, set cookie PHP with Setcookie function to set cookies. It is important to note that a cookie is a part of the HTTP protocol header that is used to pass information between the browser and the server ...

  • 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.