setcookie
发送cookie
setcookie
PHP 4, PHP 5, PHP 7, PHP 8
setcookie() 函数用于向客户端发送一个 HTTP cookie。这个 cookie 会随着后续请求被自动发送回服务器。setcookie() 必须在发送任何其他输出之前调用。
setcookie(string $name, string $value = "", int $expires = 0, string $path = "", string $domain = "", bool $secure = false, bool $httponly = false): bool
如果成功设置了 cookie,则返回 true,否则返回 false。
setcookie("username", "JohnDoe", time() + 3600, "/");
该示例设置了一个名为 username 的 cookie,值为 JohnDoe,有效期为当前时间加 3600 秒(即 1 小时),路径为整个网站根目录(/),这意味着整个网站都能访问该 cookie。