简体   繁体   中英

No cookie value on other pages

The page im posting to has the following code, and echo's the cookie correctly:

/* verify.php */

if ($age >= "21"  && $location == "USA" && $cookie == "Y") {

    $value = "1";
    setcookie("age_verified", $value, time()+60*60*24*30);
    header("Location: ../portal.php?cookieset");
}
elseif ($age >= "21" && $location == "USA") {

    session_start();
    $_SESSION['age_verified'] = "1";
    header("Location: ../portal.php?sessionset");   
}

On portal.php i am not able to echo the cookie, but the session shows up fine if that option is chosen.

/* portal.php */

session_start();
echo $_SESSION["age_verified"];

Result is "1"

/* portal.php */

echo $_COOKIE["age_verified"];

No Result

I'm trying to achieve something like the code block below, but it's not working properly since cookie doesn't echo a result /* portal.php */ session_start();

if($_SESSION['age_verified']!="1"){
    header("Location: index.php?no_session");
}
elseif ($_COOKIE['age_verified']!="1"){
    header("Location: index.php?no_cookie");
}
else{
    echo "";    
}

What am i missing?

It would seem to me that $_SESSION['age_verified']!="1"||$_COOKIE['age_verified']!="1" is checking against EITHER session or cookie values. Cookie values are more persistent as they are stored on the user's machine, and session values only persist to a browsing session. They may not both be set.

In fact, looking at your logic on verify.php you are performing one action OR the other, not both. Hope this helps.

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