setcookie - Send Cookie.
grammar:
setcookie ( string $name [, string $value = "" [, int $expire = 0 [, string $path = "" [, string $domain = "" [, bool $secure = false [, bool $httponly = false ]]]]]] )
setcookie () defines a Cookie, and the rest will be sent with HTTP headers to the client. Before transmission, and other HTTP headers Cookie, you must produce any output in the script (due to limitations of the protocol). Call this function before any output is generated (and including <head> or space <html>). Once Cookie settings, you can use the next $ _COOKIE read when you open the page. Cookie values also exist in $ _REQUEST global variables in.
parameter
parameter | Necessary | description |
---|---|---|
name | Yes | Cookie name. |
value | no | Cookie values. This value is stored in the user's computer, do not store sensitive information. Such name is 'cookiename', its value can be obtained by $ _COOKIE [ 'cookiename']. |
expire | no | Cookie expiration time. This is a Unix timestamp, namely (GMT January 1, 1970 00:00:00) the number of seconds since the Unix epoch. In other words, you can use the basic time () function result plus the number of seconds hopes expired. Or you may use the mktime () . time () + 60 * 60 * 24 * 30 is disposed Cookie 30 days expire. If set to zero, or omit parameters, Cookie expire (when the browser is turned off) at the end of the session. |
path | no | Cookie valid server path. Set to '/' is, Cookie domain effective for the entire domain. If set to '/ foo /', Cookie only the domain in / foo / active directory and its subdirectories (such as / foo / bar /). The default value is the current directory when setting up Cookie. |
domain | no | Cookie valid domain / sub-domain name. Arranged subdomains (e.g. 'www.example.com'), Cookie will take effect for the domain name and its three sub-domain (e.g. w2.www.example.com). Cookie make valid for the entire domain (including all its subdomain), the domain name as long as it is arranged (this example is 'example.com'). |
secure | no | This Cookie is set just over a secure HTTPS connection to the client. When set to TRUE, set the Cookie will exist only when a secure connection. If this demand is processed on the server side, the programmer need only send such cookies (by $ _SERVER [ "HTTPS"] is determined) on the secure connection. |
httponly | no | Set to TRUE, Cookie only accessible via HTTP protocol. This means that Cookie can not access through a similar JavaScript scripting languages. To effectively reduce identity theft when the XSS attack, may recommend using this setting (although not all browsers support), but this argument is often controversial. PHP 5.2.0 added. TRUE or FALSE |
return value
If you call this function before it produces output, setcookie () calls fail and return FALSE. If setcookie () successfully runs, returns TRUE. Of course, it did not mean the user has to accept Cookie.
$value = 'something from somewhere'; setcookie("TestCookie", $value); setcookie("TestCookie", $value, time()+3600); /* 1 小时过期 */ setcookie("TestCookie", $value, time()+3600, "/~rasmus/", "example.com", 1);
related functions
header ()
- Native HTTP header sent
setrawcookie ()
- Send a cookie without URL encoding