Collect PHP operation cookies

Source: Internet
Author: User
Tags setcookie

1:

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

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 );The domain name can be in the format of "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,ProgramThis is transparent, 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:PHP cookie2 operation, receiving and processing 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 way is to reference the global variables of PHP.$ Http_cookie_varsArray.

Echo $ mycookie;
Echo $ cookiearray [0];
Echo count ($ cookiearray );
Echo $ http_cookie_vars ["mycookie"];

3

      • cookie usage restrictions

        first, it must be set before HTML file content is output.

        second, different browsers may not provide different speeches on cookie processing, and sometimes error results may occur. For example, Ms ie + Service Pack 1 cannot correctly process cookies with domain names and paths. Netscape Communicator 4.05 and Ms ie 3.0 cannot correctly process cookies without path and time. As 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 restriction is on the client. A browser can create a maximum of 30 cookies, each of which cannot exceed 4 kb. Each web site can set a maximum of 20 cookies.

        let's talk about how PHP operates cookies.

    • There are two methods to delete an existing COOKIE:

      one is to call a 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

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.