PHP无法修改标题信息 - 已由[重复]发送的标题

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.

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.

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>
";
    }else{
       header('HTTP/1.1 303 See other');
       header('Location: ' . $url);
    }
 }

 redirect_to('http://www.test.com/');

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>

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>