开发者

PHP header Location works in the middle of HTML

开发者 https://www.devze.com 2023-01-03 21:46 出处:网络
I\'m having problems understanding how a header(\"Location: http://www.google.com/\"); can work in the middle of a HTML page\'s <body>.

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>
</h开发者_运维知识库tml>

Any ideas? Thanks.


It's because of output buffering...


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


If you have output buffering enabled (see ob_start()), no output is sent until the whole page is finished.


I noticed this recently when doing some dev with a WAMP package on Windows. Caused a lot of hell when I moved to running it on a Linux box. I believe there was a config value set causing it to buffer all pages.

0

精彩评论

暂无评论...
验证码 换一张
取 消