简体   繁体   中英

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

What's the difference between $_SERVER['HTTP_COOKIE'] and $_COOKIE ?

Why $_SERVER['HTTP_COOKIE'] isn't documented in the PHP Manual?

I'm creating a class for managing cookies, and I want to make a function that destroy all cookies that are set. I must destroy from $_SERVER['HTTP_COOKIE'] variable, like:

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:

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'] . On my share host, they don't have this variable available. However, $_COOKIE variable is usually guaranteed available.

You shouldn't use the value in $_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

actually $_SERVER is a global variable that include these global variables

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

its possible to user $_SERVER to get cookies but Not standard and maybe you get conflicts in your project

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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