Home  >  Article  >  Backend Development  >  php setcookie(name, value, expires, path, domain, secure) parameters_PHP tutorial

php setcookie(name, value, expires, path, domain, secure) parameters_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:02:16796browse

setcookie() 定义一个和其余的 HTTP 标头一起发送的 cookie。和其它标头一样,cookie 必须在脚本的任何其它输出之前发送(这是协议限制)。这需要将本函数的调用放到任何输出之前,包括 和 标签以及任何空格。如果在调用 setcookie() 之前有任何输出,本函数将失败并返回 FALSE。如果 setcookie() 函数成功运行,将返回 TRUE。这并不说明用户是否接受了 cookie。
函数定义:
bool setcookie ( string name [, string value [, int expire [, string path [, string domain [, bool secure]]]]] )
setcookie() 参数详解

参数        说明 举例
name cookie的名字 使用 $_COOKIE['cookiename'] 调用名为 cookiename 的 cookie。
value cookie的值,存放在客户端,不要存放敏感数据 假定 name 是 'cookiename',可以通过$_COOKIE['cookiename'] 取得其值。
expire

Cookie 过期的时间。这是个 Unix 时间戳,即从 Unix 纪元开始的秒数。  

换而言之,通常用 time() 函数再加上秒数来设定 cookie 的失效期。

或者用mktime()来实现。

time()+60*60*24*30 将设定 cookie 30 天后失效。

如果未设定,cookie 将会在会话结束后(一般是浏览器关闭)失效。

path Cookie 在服务器端的有效路径。

如果该参数设为 '/' 的话,cookie 就在整个 domain 内有效,

如果设为 '/foo/',cookie 就只在 domain 下的 /foo/ 目录及其子目录内有效,例如 /foo/bar/

默认值为设定 cookie 的当前目录。

domain 该 cookie 有效的域名。

要使 cookie 能在如 example.com 域名下的所有子域都有效的话,该参数应该设为 '.example.com'

虽然 . 并不必须的,但加上它会兼容更多的浏览器。

如果该参数设为www.example.com 的话,就只在 www 子域内有效。

细节见Cookie 规范中的 tail matching。

secure

指明 cookie 是否仅通过安全的 HTTPS 连接传送。

当设成 TRUE 时,cookie 仅在安全的连接中被设置。默认值为FALSE

0 或 1

Example 1. setcookie() sending example
Copy code The code is 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);

Pay attention to the value of the cookie The part will be automatically urlencoded when sent and automatically decoded when received and assigned the value to the cookie variable with the same name. If you don't want this and are using PHP 5, you can use setrawcookie() instead. The following simple example can get the value of the cookie just set:
Copy the code The code is as follows:

// Output a separate cookie
echo $_COOKIE["TestCookie"];
echo $HTTP_COOKIE_VARS["TestCookie"];
// Another debugging method is to output All cookies
print_r($_COOKIE);
?>

To delete a cookie, you need to ensure that its expiration date is in the past to trigger the browser's deletion mechanism. The following example illustrates how to delete the cookie 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 the array symbol in the cookie name. Multiple cookies can be set as array units. When the script extracts cookies, all values ​​are placed in an array:
Example 3. Example of using arrays in setcookie()
Copy code The code is as follows:

// Set cookie
setcookie("cookie[three]" , "cookiethree");
setcookie("cookie[two]", "cookietwo");
setcookie("cookie[one]", "cookieone");
// After refreshing the page, it will be displayed out
if (isset($_COOKIE['cookie'])) {
foreach ($_COOKIE['cookie'] as $name => $value) {
echo "$name : $value
n";
}
}
?>

The above example will output:
three : cookiethree
two : cookietwo
one : cookieone

Summary: The basic use of cookies is not difficult. The focus of this article is mainly to master the path setting and domain of the path. domain name settings.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327934.htmlTechArticlesetcookie() Defines a cookie that is sent along with the rest of the HTTP headers. Like other headers, the cookie must be sent before any other output from the script (this is a protocol restriction). This...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn