PHP header location-redirect doesn't work - why?

Industrial picture Industrial · Apr 25, 2010 · Viewed 90.1k times · Source

Here's my file. I want to make it redirect, but nothing happens. To check out what is going on, I added an echo before the header part.

It neither throws an error or redirect to index.php. What is wrong? I have turned output buffering on/off, but nothing makes it redirect. What can I do?

<?
error_reporting(E_ALL);
echo 'This is an error';

header("Location: login.php");
die();
?>

Thanks

Answer

Soufiane Hassou picture Soufiane Hassou · Apr 25, 2010

From PHP documentation :

header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.

And in your case, you are using echo before header()