PHP header redirect not working on Apache
This is my first question here, and I will try to follow all the advice I received when I signed up...
My problem is the 'headers sent' error when trying to do a PHP redirect.
I have this tiny script to test a redirect:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" >
</head>
<body>
<?php
if (isset($_POST['go']) && $_POST['go'] == 'go') {
$redirectURL = 'test1.php';
header( 'Location: ' . $redirectURL ) ;
exit();
}
echo '<p>This is Page 1. We will test a redirect. Type in \'go\' and hit submit:</p><br><br>';
echo '<form name="test" method="post" action="' . $_SERVER['PHP_SELF'] . '">';
echo '<input type="text" name="go">';
echo '<input type="submit" name="submit">';
echo '</form>';
?>
</body>
</html>
It redirects fine when run on a Windows server, but on an Apache it gives the error:
Warning: Cannot modify header information - headers already sent by (output started at /home/simply92/public_html/test/testUTF.php:7) in /home/simply92/public_html/test/testUTF.php on line 10
Line 7 is the php tag.
I used my web host's code editor to save with encoding ISO-8859-1 (which should get any BOMs out of the equation, right?) and I set the default charset in php.ini to ISO-8859-1 as well (in case that matters.)
One strange thing is that it redirects even on the Apache when I delete doctype and everthing el开发者_JAVA百科se up to the php tag and have my very first line in the script be <?php
. Obviously that can not be the solution.
So my question is: How could I find out WHAT is being sent before my header() call, and then, how could I get rid of it?
Hopefully someone can point me in the right direction!
Thanks in advance.
PHP's header requires that the output buffer is free from any other data. The headers must be set before the doctype, head and body tags printed. This should work
<?php
if (isset($_POST['go']) && $_POST['go'] == 'go') {
$redirectURL = 'test1.php';
header( 'Location: ' . $redirectURL ) ;
exit();
}
?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" >
</head>
<body>
<?php
echo '<p>This is Page 1. We will test a redirect. Type in \'go\' and hit submit:</p><br><br>';
echo '<form name="test" method="post" action="' . $_SERVER['PHP_SELF'] . '">';
echo '<input type="text" name="go">';
echo '<input type="submit" name="submit">';
echo '</form>';
?>
</body>
</html>
This is one of the most famous errors for php, you can make a search on Google for explanations
In your case your code is wrong because you DO a lot of output before the header
call. In windows it just works because you likely have the output buffering turned on and you haven't it in Apache.
You can read about it here, but I really advise you to change your code, the header
function should be called only if you haven't sent any output to the browser, you can check for it by using the header_sent function.
精彩评论