如果Cookie不等于变量,如何重定向

I am struggling to redirect the user if the cookie does not equal a vairable. If it does equal the vairable, then it should continue the script. Here is my code to redirect :

if(empty($_GET)) {
    //No variables are specified in the URL.
    //Do stuff accordingly
    echo "No variables specified in URL...";
} else {
    //Variables are present. Do stuff:
    $id = htmlspecialchars($_GET["id"]);
    echo 'url query is ' . $id;
}

if(isset($_COOKIE['logged_in']) == $id)
{
    header("Location: test.php");
}
if(isset($_COOKIE['logged_in']) != $id)
{
    //continues the script

Please note that the vairable in the if statment ($id) is a vairable from the query of url; for example if the url is, "random.com/test.php?id=17" and the cookie equals 18 the script should redirect. However if url is, "random.com/test.php?id=17" and the cookie equals 17, then stay on the same page. Sorry if it sounds complecated.

It doesnt work as this code: It doesnt redirect no matter what the vairable equals. Thanks

you should add another condition.

 if(empty($_GET)) {
    //No variables are specified in the URL.
    //Do stuff accordingly
    echo "No variables specified in URL...";

} else {
    //Variables are present. Do stuff:
    $id = htmlspecialchars($_GET["id"]);
    echo 'url query is ' . $id;
}

if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in'] == $id)
{
    header("Location: test.php");
}
if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in'] != $id)
{
    //continues the script

or use this script

if(isset($_COOKIE['logged_in']))
{
   if($_COOKIE['logged_in']==$id){
       header("Location: test.php");
   }
   else{
       //another condition to equal is not equal so directly we can use else
       //continues the script
   }
} else {
echo "Cookie not valid or available";
// redirect user 
}

Are you looking for something like this. If so, it should work for your case:

<?php
if(empty($_GET)) {
    //No variables are specified in the URL.
    //Do stuff accordingly
    echo "No variables specified in URL...";
} else {
    //Variables are present. Do stuff:
    $id = htmlspecialchars($_GET["id"]);
    echo 'url query is ' . $id;
}

if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in']==$id)
{
    header("Location: test.php");
}
if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in']!=$id)
{
    //continues the script
}
?>

A headers will apply only after it send to client. If you want immediately redirect, you can put exit(0) after header(...) in this case you are stop executing of the script and will send current headers to the browser which will redirect you.

if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in']==$id) {
    header("Location: test.php");
    exit(0);
}

//continues the script

The problem is that you are comparing the "value" of isset (the result) with the value of your GET parameter, $id:

if(isset($_COOKIE['logged_in']) == $id)

What this says is "determine if $_COOKIE['logged_in'] is set and compare that determination to $id". PHP will evaluate isset, which returns true or false (as it says in the documentation), and compare that true or false to the other side of the expression (==), meaning $id, which will never match given your examples. If you query "random.com/test.php?id=true" (or false) that might do what you are looking for.

The line you have does not mean "determine if $_COOKIE['logged_in'] is set and compare the value of $_COOKIE['logged_in'] to the value of $id", which I believe is what you are looking for. In that case, what you want to do is first check that $_COOKIE['logged_in'] is set and then check that the value of $_COOKIE['logged_in'] matches $id, like so:

if (isset($_COOKIE['logged_in']) && $_COOKIE['logged_in'] == $id)

If that doesn't make sense, here is a really explicit version that might be clearer as to what is actually going on:

if ((isset($_COOKIE['logged_in']) == true) && ($_COOKIE['logged_in'] == $id))

Hope that helps.