Setcookie () defines a cookie that is sent along with the rest of the HTTP headers. As with other headers, cookies must be sent before any other output of the script (this is the protocol limit). This requires that the call to this function be placed before any output, including the
function Definition:
bool Setcookie (string name [, string value [, int expire [, String path [, string domain [, bool secure]]]
Setcookie () parameter detailed
Parameters |
Description |
Example |
Name |
The name of the cookie |
Use$_COOKIE['cookiename'] 调用名为 cookiename 的 cookie。 |
Value |
The value of the cookie, stored on the client, do not store sensitive data |
Assumename 是 'cookiename',可以通过$_COOKIE['cookiename'] 取得其值。 |
Expire |
Time the Cookie expires. This is a Unix timestamp, that is, the number of seconds from the start of the Unix era. In other words, the expiration period of the cookie is usually set with the time () function plus the number of seconds. or use mktime () to achieve. |
time()+60*60*24*30 将设定 cookie 30 天后失效。 If it is not set, the cookie will expire after the session is closed (typically browser shutdown). |
Path |
Valid path for cookies on the server side. |
If the parameter is set to'/' 的话,cookie 就在整个 domain 内有效, If set to'/foo/',cookie 就只在 domain 下的 /foo/ 目录及其子目录内有效,例如 /foo/bar/。 The default value is the current directory of the cookie set. |
Domain |
A valid domain name for this cookie. |
For a cookie to work in all subdomains, such as the example.com domain name, this parameter should be set to'.example.com'。 Although. 并不必须的,但加上它会兼容更多的浏览器。 If the parameter is set towww.example.com 的话,就只在 www 子域内有效。 Details see the tail matching in the cookie specification. |
Secure |
Indicates whether the cookie is transmitted only through a secure HTTPS connection. When set to TRUE , cookies are set only in secure connections. The default value is FALSE. |
0 或 1 |
Example 1 Setcookie () Send example
Copy Code code as follows:
$value = ' something from somewhere ';
Setcookie ("TestCookie", $value);
Setcookie ("TestCookie", $value, Time () +3600); /* expire in 1 hour * *
Setcookie ("TestCookie", $value, Time () +3600, "/~rasmus/", ". utoronto.ca", 1);
Note that the portion of the value in the cookie is automatically encoded with UrlEncode when it is sent and automatically decoded when it is received and assigned the value to a cookie variable of its own name. If you don't want to do this and you're using PHP 5, you can use Setrawcookie () instead. Here's a simple example that gets the value of the cookie you just set:
Copy Code code as follows:
<?php
Output a separate cookie
echo $_cookie["TestCookie"];
echo $HTTP _cookie_vars["TestCookie"];
Another way to debug is to output all cookies
Print_r ($_cookie);
?>
To remove a cookie, you need to ensure that its expiration period is in the past in order to trigger the browser's deletion mechanism. The following example shows how to delete a cookie that you just set:
Example 2 setcookie () Delete Example
Copy Code code as follows:
Set the expiration time to one hour ago
Setcookie ("TestCookie", "" ", Time ()-3600);
Setcookie ("TestCookie", "", Time ()-3600, "/~rasmus/", ". utoronto.ca", 1);
You can also set an array cookie by using an array symbol in the cookie name, and you can set multiple cookies as array cells, and all values are placed in an array when the script extracts the cookie:
Examples of using arrays in Example 3 Setcookie ()
Copy Code code as follows:
<?php
Set cookies
Setcookie ("Cookie[three]", "Cookiethree");
Setcookie ("Cookie[two]", "cookietwo");
Setcookie ("Cookie[one]", "Cookieone");
When the page is refreshed, it appears
if (Isset ($_cookie[' COOKIE ')) {
foreach ($_cookie[' COOKIE '] as $name => $value) {
echo "$name: $value <br/>\n";
}
}
?>
The example above will output:
Three:cookiethree
Two:cookietwo
One:cookieone
Summary:the basic use of cookies is not difficult, this article records the main focus is to master path settings and domain name settings in domain.