Reputation: 41
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.
Upvotes: 4
Views: 4317
Reputation: 15045
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
Upvotes: 5
Reputation: 11
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
Upvotes: 0
Reputation: 11
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");
}
?>
Upvotes: 0