Set-cookie of PHP Header

Source: Internet
Author: User

The PHP settings cookie can use the Setcookie function or the header function. You need to pay attention to the order of the strings when using the header method, and unexpected problems may occur if the order is incorrect. The correct order is

Name=value; [Expires=date; [Path=path]; [Domain=domainname]; [Secure]]

For example: Under Netingcn.com, set a cookie named CookieName, with a value of cookievalue, and an expiration time of one year

Header ("Set-cookie:cookiename=cookievalue; Expires= ". Gmstrftime ("%A,%d-%b-%y%h:%m:%s GMT", Time () + (86400 * 365)).  path=/; Domain=netingcn.com ');

If you put domain, path in front of the expires, when Firefox through the Httpfox view, found that the expiration time is "end of the session", but after exiting the browser to reopen, found that the cookie still exists, Chrome also shows similar. Although browsers eventually set a one-year expired cookie, domain information is the full domain of the current page, not the top-level domain of the setting. So be sure to pay attention to the order.

In addition, if you set multiple cookies in header mode, you need to set the second parameter of the header to false, and the following is the header function declaration:

void header (String string [, bool replace [, int http_response_code]])

The second parameter, replace, indicates whether to replace the previous similar header or to add a header of the same type. The default is replace, but if you set it to FALSE, you can force multiple homogeneous headers to be sent.


Http://www.netingcn.com/php-header-set-cookie.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.