Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP header Location works in the middle of HTML

I'm having problems understanding how a header("Location: http://www.google.com/"); can work in the middle of a HTML page's <body>.
Shouldn't there be an error since the header has already been sent due to the HTML output, way before the <?php ... ?> part started.

I'm referring to the warning Cannot modify header information - headers already sent by... that I'm expecting to get.

I'm testing this on my local PHP dev environment (Apache/2.2.15 (Win32) PHP/5.3.2).

Here's an example:

<html>
<head>
</head>
<body>
<?php header("Location: http://www.google.com/"); ?>
</body>
</html>

Any ideas? Thanks.

like image 205
tshabala Avatar asked Jan 22 '23 22:01

tshabala


2 Answers

It's because of output buffering...

like image 91
ircmaxell Avatar answered Jan 31 '23 06:01

ircmaxell


If you have output buffering autoenabled in php.ini then you can emit headers at any time before the output is actually sent.

like image 37
Ignacio Vazquez-Abrams Avatar answered Jan 31 '23 06:01

Ignacio Vazquez-Abrams