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.

user1985649
  • 41
  • 1
  • 2
  • is it at the top of index.html ? –  Jan 17 '13 at 01:40
  • make sure there is nothing outputted in the browser before you do the redirection, this means that even if the redirection is in the topbost part of the page but that page is being included / called by another file then most likely something has already been outputted on the browser – ianace Jan 17 '13 at 01:42
  • BTW, with `Location:`, you should be using absolute paths, so it should be `http://www.example.com/index.html`, not `index.html`. – nickb Jan 17 '13 at 01:46
  • Also, make sure you either `die()` or `exit()` after the location header is sent. Choosing to follow location headers is up to the client and it is pretty easy to force the client to not follow them. If that is the case, then any code after can still be executed. – sberry Jan 17 '13 at 01:47

3 Answers3

5

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
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
kittycat
  • 14,983
  • 9
  • 55
  • 80
  • So it isn't possible to use it in an html document? That could be apart of my problem. I will use your code and see it works for me, thanks. And also, if my homepage is written in HTML, can I just change the tag to – user1985649 Jan 17 '13 at 02:36
  • @user1985649 You keep the HTML all the same, just make sure at the **TOP** of the page you put the above code inside `` tags WITHOUT any leading spaces, or content before the ` – kittycat Jan 17 '13 at 03:02
  • I really feel quite newbie, and I will openly admit to it. But I have done as you said, and I get a server error as if there is something wrong with the code. But basically, my current layout looks like this:

    Is there something I'm doing wrong here?

    – user1985649 Jan 17 '13 at 03:27
  • @user1985649 also if you are not wanting to display any content in index.php and want to just use it to redirect you should not be putting anything inside it but the PHP code. – kittycat Jan 17 '13 at 03:36
  • Never mind my last request, but yes, I was trying to display both content and use the script within the document. But I'll take the easier, less professional route and just rename my index.html to something else and the php in a seperate index.php so that if the cookie is found, it will redirect to either the agreement or the homepage. ---- On second thought, JavaScript isn't server sided, is it? Could I use JavaScript in my index.html to detect a cookie that was created with PHP? – user1985649 Jan 17 '13 at 03:42
  • @user1985649 yes you can use JS to detect a cookie provided it was not set with the HttpOnly flag. See [this](http://stackoverflow.com/questions/5968196/check-cookie-if-cookie-exists/5968306#5968306) post on how to access a cookie using JavaScript. However PHP redirect would be a cleaner and simpler solution. – kittycat Jan 17 '13 at 04:03
  • It was error 500. So this is the PHP i have: ?> and i got that error. Nothing else in the document. You also said that I could rename my document to .php from .html. Does this still work if there is HTML content in it? Just change it to .php? Or that and change the tag to – user1985649 Jan 17 '13 at 04:16
0

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");
}
 ?>
0

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