mardi 26 juillet 2016

PHP checks cookie value for response, Response won't revert back after cookie is cleared

So I've scoured w3schools and this fine website for some time but I can't figure this out.

I have a script that creates a cookie in the users session upon loading the browser, and another script that will check the value of said cookie and respond with 2 echo's depending on the value.

Script to create the cookie is:

<?php
$cookie_name = "CTF";
$cookie_value = "ChangeThis";
if(!isset($_COOKIE[$cookie_name])) {
setcookie($cookie_name, $cookie_value, time() + 86400, "/", NULL);
}
?>

My IF check for the values is as follows:

<?php
if($_COOKIE[$cookie_value] = "CTF") {
    echo "Congratulations. Your token code is 4HeWPzK63Lf5NkGS";
    } else { 
    echo "Your cookie is not set yet.";
    }
?>

The code works, and when the cookie is changed to CTF it echo's the correct line. However, when I then delete the cookie and refresh the page, essentially resetting the cookie back to the value of "ChangeThis", the first echo still appears on the page.

Can someone point me in the right direction?

Aucun commentaire:

Enregistrer un commentaire