1

Possible Duplicate:
PHP error: Cannot modify header information – headers already sent
Headers already sent by PHP

This is my code:

<html>
<body>
    <?php

    if($country_code == 'US')
    {
        header('Location: http://www.test.com');
    }

    else 
    {
        header('Location: http://www.test.com/');
    }

    ?>

<script language="JavaScript" type="text/javascript"></script>

</body>
</html>

No space before <?php or after ?>

I have tried placing the HTML code and Javascript below the PHP altogether but then that would make it not track clicks to the page.

Community
  • 1
  • 1
user1887109
  • 69
  • 1
  • 8

4 Answers4

4

Nothing should be output before your headers. Headers are always sent before content. You cannot output anything before making a call to header(), and expect it to work. (Some servers can have output buffering enabled which works around the problem, but it doesn't solve anything, and isn't good to rely on.)

Your note about tracking clicks to a page is nonsense. Most browsers won't bother rendering HTML when given a 301 or 302 status code with a Location: header.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • How is it nonsense? I'm using a tracking program and the Javascript is the code it gives to track clicks and it has worked by the way, just having issue with combining it with PHP. – user1887109 Dec 16 '12 at 23:33
  • It is nonsense because the server process the php-code before anything has given to the clients-browser. So don't write the `header()` information into your HTML-header. – wildhaber Dec 16 '12 at 23:57
  • 1
    You need to understand the basics of how HTTP works. Headers are sent before document content. Making a `header()` call in the middle of the page does not change where that header actually appears. You **must** send headers before the content. Again, I don't know what this JavaScript is that you're running, but most browsers won't bother with the HTML if they are being redirected, and your JavaScript won't run. If any content is sent with a `3xx` status code, it should just be a friendly message that indicates where the user is being redirected to. – Brad Dec 16 '12 at 23:58
  • Who downvoted this and why? I've about had it with folks downvoting correct answers and offering no explanation as to why. – Brad Dec 17 '12 at 00:00
1

If your main worry is the javascript tracking code then I would suggest a javascript redirect:

<?php
// pre-html PHP code
?><!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="trackingScript.js"></script>
<script type="text/javascript">
<?php
if($country_code == 'US'){
  echo "document.location.href = 'http://www.test.com/1';";
}else{
  echo "document.location.href = 'http://www.test.com/2';";
}
?>
</script>
</head>
<body></body>
</html>
Derek
  • 4,864
  • 5
  • 28
  • 38
0

yes headers can't be sent after start of output , something like this can resolve it

function redirect_to($url){
    // If the headers have been sent, then we cannot send an additional location header
    // so output a javascript redirect statement.
    if (headers_sent()){
       echo "<script>document.location.href='" . htmlspecialchars($url) . "';</script>\n";
    }else{
       header('HTTP/1.1 303 See other');
       header('Location: ' . $url);
    }
 }

 redirect_to('http://www.test.com/');
Fivell
  • 11,829
  • 3
  • 61
  • 99
0

You can put the PHP code at the beginnig of the file.

<?php
if($country_code == 'US')
{
    header('Location: http://www.test.com');
}
else 
{
    header('Location: http://www.test.com/');
}

?>
<html>
<body>

<script language="JavaScript" type="text/javascript"></script>

</body>
</html>
Sergio Flores
  • 5,231
  • 5
  • 37
  • 60