简体   繁体   中英

PHP if(cookie is set Redirect loop

So, I'm having trouble with quite simple PHP, as I am not adept in anyway with it. So here's the code:

 <?php 
if(!isset ($_COOKIE['cookie'])) {
 header("Location: index.html");
} else {
 header("Location: index2.php");
 ?>

It's at the top of an HTML document, before any other HTML because I've heard header won't work otherwise (that statement could prove my ignorance itself). But basically, I have an agreement page you must agree to before continuing to the site, but it's not considered my index file. So I need this redirect to detect the if the cookie that is set by the agreement.php exists or not, and I assume that this syntax is correct, but it seemingly doesn't work. I used an echo "

Any ideas on how to fix? Thank you in advance.

Try using this code:

index.php should be this (start of file to end)

<?php

if (!isset($_COOKIE['cookie']))
{
    header('Location: http://www.example.com/index2.php');
    exit;
}

?>
<!DOCTYPE html>
...rest of your HTML code

you are also missing the closing curly brace on your else

<?php 
if(!isset ($_COOKIE['cookie']))
 {
 header("Location: index.html");
} 
else 
{
 header("Location: index2.php");
}
 ?>

I would rather use something along these lines

 <?php

 echo $htmlHeader;

 while($stuff){

 echo $stuff;

  }

 echo "<script>window.location = 'url'</script>";
  ?>

It works nice for me

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