New PHP Cookie setting method secrets

Source: Internet
Author: User

PHP has been developing for a long time and many users are familiar with PHP. Here I will post on PHP Cookie settings. PHP uses the SetCookie function to set cookies. Note that Cookie is a part of the HTTP header used to transmit information between the browser and the server. Therefore, the Cookie function must be called before any HTML file is output. 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:

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

The PHP Cookie settings on the same page are actually from the back to the back. Therefore, if you want to delete a new Cookie before inserting it, you must first write the insert statement and then write the delete statement, otherwise, unexpected results may appear. Let's take a look at several examples of simple PHP Cookie settings:

 
 
  1. SetCookie("MyCookie", "Value of MyCookie"); 

With expiration time:

 
 
  1. SetCookie ("WithExpire", "Expire in 1 hour", time () + 3600); // 3600 seconds = 1 hour

Everything:

 
 
  1. SetCookie("FullCookie", "Full cookie value", time()+3600, "/forum", ".phpuser.com", 1); 

Here is another note. For example, if your site has several different directories, if you only use cookies without paths, the Cookie set on the page under one directory is invisible on the page of another directory, that is, the Cookie is path-oriented. In fact, even if no path is specified, the WEB server will automatically pass the current path to the browser, and the specified path will force the server to use the set path. To solve this problem, add the path and domain name when calling SetCookie. The domain name format can be "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, this is transparent to programs, because PHP will automatically decode the Cookie value when receiving it.

To set multiple cookies with the same name, use an array:

 
 
  1. SetCookie("CookieArray[]", "Value 1");  
  2. SetCookie("CookieArray[]", "Value 2"); 

Or

 
 
  1. SetCookie("CookieArray[0]", "Value 1");  
  2. SetCookie("CookieArray[1]", "Value 2");  

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.