简体   繁体   English

$_SERVER['HTTP_COOKIE'] 和 $_COOKIE 的区别

[英]Difference between $_SERVER['HTTP_COOKIE'] and $_COOKIE

What's the difference between $_SERVER['HTTP_COOKIE'] and $_COOKIE ? $_SERVER['HTTP_COOKIE']$_COOKIE 有什么区别?

Why $_SERVER['HTTP_COOKIE'] isn't documented in the PHP Manual?为什么$_SERVER['HTTP_COOKIE']没有记录在 PHP 手册中?

I'm creating a class for managing cookies, and I want to make a function that destroy all cookies that are set.我正在创建一个用于管理 cookie 的类,我想创建一个函数来销毁所有设置的 cookie。 I must destroy from $_SERVER['HTTP_COOKIE'] variable, like:我必须从 $_SERVER['HTTP_COOKIE'] 变量中销毁,例如:

public function destroy_all() {
    $cookies = explode(';', $_SERVER['HTTP_COOKIE']);
    foreach($cookies as $cookie) {
        $parts = explode('=', $cookie);
        $name = trim($parts[0]);
        setcookie($name, null, 1, $this->cookie_path);
    }
}

or with $_COOKIE array, like:或使用 $_COOKIE 数组,例如:

public function destroy_all() {
    foreach($_COOKIE as $name => $cookie) {
        setcookie($name, null, 1, $this->cookie_path);
    }
}

Not all server has the this global variable $_SERVER['HTTP_COOKIE'] . 并非所有服务器都具有此全局变量$_SERVER['HTTP_COOKIE'] On my share host, they don't have this variable available. 在我的共享主机上,他们没有此变量可用。 However, $_COOKIE variable is usually guaranteed available. 但是, $_COOKIE变量通常可以保证可用。

You shouldn't use the value in $_SERVER["HTTP_COOKIE"] . 您不应该使用$_SERVER["HTTP_COOKIE"] It's not documented, so it's probably not reliable. 它没有记录,所以它可能不可靠。 more details click here .Note that the $_COOKIE variable not will hold multiple cookies with the same name 更多详情请点击这里 。注意$_COOKIE变量不会包含多个具有相同名称的cookie

actually $_SERVER is a global variable that include these global variables实际上 $_SERVER 是一个包含这些全局变量的全局变量

$_SERVER = $_COOKIE + $_SESSION + $_GET + $_POST $_SERVER = $_COOKIE + $_SESSION + $_GET + $_POST

its possible to user $_SERVER to get cookies but Not standard and maybe you get conflicts in your project用户 $_SERVER 可以获取 cookie 但不是标准的,也许你的项目会发生冲突

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM