3

I am trying to show some page that automatically redirect to another page, when using header(location) I get this error:

Warning: Cannot modify header information - headers already sent by (output started at public_html/headerFile.php)

What else should I try?

Machavity
  • 30,841
  • 27
  • 92
  • 100
eomeroff
  • 9,599
  • 30
  • 97
  • 138

3 Answers3

10

The longer, and more correct way, is to reorganize your logic so that it knows that it should redirect the user before it tries outputting anything to the page. Do all of your logic first, then display.

The quick easy way around is to add ob_start() to the beginning of your script. This turns on output buffering, so nothing gets sent to the browser until everything is finished, which means you can still modify the headers.

Tesserex
  • 17,166
  • 5
  • 66
  • 106
4

Try this:

echo '<meta http-equiv="Refresh" content="1;URL=http://www.google.nl">';

That should work, it's not ideal. But it will do the trick.

Wesley
  • 798
  • 3
  • 8
  • 15
0

When using header(), you have to make sure that you have not sent any output to the user. This includes print_r, echo or any errors or warnings generated by PHP. Even whitespace counts as output.

shmeeps
  • 7,725
  • 2
  • 27
  • 35