Home » Php » Cookie not registered at http (in php)

Cookie not registered at http (in php)

Posted by: admin February 25, 2020 Leave a comment

Questions:

The cookie is stored in https but not registered in http

header('Cache-Control: no-store');
var_dump($_COOKIE);
function ishttp(){
    $t=$_SERVER['REQUEST_SCHEME'];
    echo $t;
    if ($t=='http'){return 'http';}
    else {return 'https';}
}
if (ishttp()=='http'){
    setcookie('val','http',time()+90000);
}
else{
    setcookie('val','https',time()+90000);
}

I tested on two hosts
There was no problem with a host

image Https output
image Http output

How to&Answers:

You should try it once with domain parameter with “/” for same domain:

 header('Cache-Control: no-store');
    var_dump($_COOKIE);
    function ishttp(){
        $t=$_SERVER['REQUEST_SCHEME'];
        echo $t;
        if ($t=='http'){return 'http';}
        else {return 'https';}
    }
    if (ishttp()=='http'){
        setcookie('val','http',time()+90000,"/");
    }
    else{
        setcookie('val','https',time()+90000,"/");
    }