目录

PHP setcookie() 函数

❮ PHP 网络参考

示例

以下示例创建一个名为 "user" 且值为 "John Doe" 的 cookie。 Cookie 将在 30 天 (86400 * 30) 后过期。 "/" 表示 Cookie 在整个网站中可用(否则,请选择您喜欢的目录)。

然后我们检索 cookie "user" 的值(使用全局变量 $_COOKIE)。我们还使用 isset() 函数来查明 cookie 是否已设置:

<!DOCTYPE html>
<?php
$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>
<html>
<body>

<?php
if(!isset($_COOKIE[$cookie_name])) {
    echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
    echo "Cookie '" . $cookie_name . "' is set!<br>";
    echo "Value is: " . $_COOKIE[$cookie_name];
}
?>

</body>
</html>
亲自试一试 »

定义和用法

setcookie() 函数定义要与其余 HTTP 标头一起发送的 cookie。

cookie 通常用于识别用户。 Cookie 是服务器嵌入到用户计算机上的一个小文件。每次同一台计算机通过浏览器请求页面时,它也会发送 cookie。使用 PHP,您可以创建和检索 cookie 值。

cookie 的名称会自动分配给同名的变量。例如,如果发送的 cookie 名称为 "user",则会自动创建一个名为 $user 的变量,其中包含 cookie 值。

笔记:setcookie() 函数必须出现在 <html> 标记之前。

笔记:cookie的值在发送cookie时自动进行URL编码,在接收时自动解码(为了防止URL编码,使用setrawcookie()反而)。

语法

setcookie( name, value, expire, path, domain, secure, httponly);

参数值

Parameter Description
name Required. Specifies the name of the cookie
value Optional. Specifies the value of the cookie
expire Optional. Specifies when the cookie expires. The value: time()+86400*30, will set the cookie to expire in 30 days. If this parameter is omitted or set to 0, the cookie will expire at the end of the session (when the browser closes). Default is 0
path Optional. Specifies the server path of the cookie. If set to "/", the cookie will be available within the entire domain. If set to "/php/", the cookie will only be available within the php directory and all sub-directories of php. The default value is the current directory that the cookie is being set in
domain Optional. Specifies the domain name of the cookie. To make the cookie available on all subdomains of example.com, set domain to "example.com". Setting it to www.example.com will make the cookie only available in the www subdomain
secure Optional. Specifies whether or not the cookie should only be transmitted over a secure HTTPS connection. TRUE indicates that the cookie will only be set if a secure connection exists. Default is FALSE
httponly Optional. If set to TRUE the cookie will be accessible only through the HTTP protocol (the cookie will not be accessible by scripting languages). This setting can help to reduce identity theft through XSS attacks. Default is FALSE


技术细节

返回值: 成功则为真。失败时为 FALSE
PHP 版本: 4+
PHP 变更日志: PHP 5.5 - Max-Age 属性包含在发送给客户端的 Set-Cookie 标头中
PHP 5.2 - 添加了 httponly 参数

更多示例

示例

Cookie 的几个到期日期:

<?php
$value = "Hello world!";

// cookie will expire when the browser close
setcookie("myCookie", $value);

// cookie will expire in 1 hour
setcookie("myCookie", $value, time() + 3600);

// cookie will expire in 1 hour, and will only be available
// within the php directory + all sub-directories of php
setcookie("myCookie", $value, time() + 3600, "/php/");
?>
<html>
<body>

...some code...

</body>
</html>
亲自试一试 »

示例

要修改 cookie,只需使用 setcookie() 函数(再次)设置 cookie:

<?php
$cookie_name = "user";
$cookie_value = "Alex Porter";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
?>
<html>
<body>

<?php
if(!isset($_COOKIE[$cookie_name])) {
    echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
    echo "Cookie '" . $cookie_name . "' is set!<br>";
    echo "Value is: " . $_COOKIE[$cookie_name];
}
?>

</body>
</html>
亲自试一试 »

示例

要删除 cookie,请使用 setcookie() 函数并指定过去的过期日期:

<?php
// set the expiration date to one hour ago
setcookie("user", "", time() - 3600);
?>
<html>
<body>

<?php
echo "Cookie 'user' is deleted.";
?>

</body>
</html>
亲自试一试 »

示例

创建一个小脚本来检查 cookie 是否已启用。首先,尝试使用 setcookie() 函数创建一个测试 cookie,然后对 $_COOKIE 数组变量进行计数:

<?php
setcookie("test_cookie", "test", time() + 3600, '/');
?>
<html>
<body>

<?php
if(count($_COOKIE) > 0) {
    echo "Cookies are enabled.";
} else {
    echo "Cookies are disabled.";
}
?>

</body>
</html>
亲自试一试 »

❮ PHP 网络参考