First of all, the browsing of the cookie operation is through the HTTP header (commonly known as "HTTP Header") to achieve. All cookie data transfer between server and client is done via HTTP request header.
PHP Setcookie (The implementation of the function) is to encapsulate the HTTP header, which seems to use the header and the use of Setcookie is the same.
Because header information belongs to the HTTP protocol content, you must first send the head information to the server, and then perform other operations such as data download, so there can be no content output before Setcookie and header (for example: echo/printf, etc.)
Header Set Cookie mode:
1 |
Header("Set-cookie:testcookie= Chinese; path=/; domain=.sunphp.org; Expires= ".Gmstrftime("%A,%d-%b-%y%h:%m:%s GMT",Time() +9600) ) ; |
Setcookie Setup mode:
Syntax: Setcookie (name,value,expire,path,domain,secure)
Cases:
1 |
Setcookie("Website",' Www.sunphp.org ',Time()+3600*24) ; |
Parameters |
Description |
Name |
Necessary. Specifies the name of the cookie. |
Value |
Necessary. Specifies the value of the cookie. |
Expire |
Optional. Specify the validity period of the cookie. |
Path |
Optional. Specifies the server path of the cookie. |
Domain |
Optional. Specifies the domain name of the cookie. |
Secure |
Optional. Specifies whether to transfer cookies through a secure HTTPS connection. |
The difference between the header setting cookie and the built-in Setcookie in PHP