4

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.

4

3 回答 3

5

尝试使用此代码:

index.php 应该是这个(文件开始到结束)

<?php

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

?>
<!DOCTYPE html>
...rest of your HTML code
于 2013-01-17T01:48:06.177 回答
0

您还缺少 else 上的右花括号

<?php 
if(!isset ($_COOKIE['cookie']))
 {
 header("Location: index.html");
} 
else 
{
 header("Location: index2.php");
}
 ?>
于 2015-03-04T12:12:50.757 回答
0

我宁愿使用这些方面的东西

 <?php

 echo $htmlHeader;

 while($stuff){

 echo $stuff;

  }

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

它对我很好

于 2015-08-31T20:03:13.953 回答