PHP header location sent even inside output buffer?

I'm having trouble suppressing the PHP location header from the output buffer. I understand that output buffers must suppress headers until they are discarded. I also thought that no headers should be sent with ob_end_clean ().

However, if you see the code below, if I uncomment the header line (second line), I always redirect to google and never see "finished".

ob_start();
//header("Location: http://www.google.com");
$output = ob_get_contents();
ob_end_clean();

$headers_sent = headers_sent();
$headers_list = headers_list();

var_dump($headers_sent);
var_dump($headers_list);

die('finished');

I need to suppress any header redirects, ideally catch them in the output buffer, so I know that these conditions will lead to a redirect. I know that I can do this with curl (setting up forwarding to false), but since all the files I want to buffer are on my own server, curl is very slow and binds the loads of db connections.

Does anyone have any suggestions or any ways to catch / suppress location headers?

Thanks Tom

+3
source share
3 answers

See if you can use header_removewith . This is similar to working with IIS / FastCGI and Apache: headers_list

<?php
ob_start();
header('Location: http://www.google.com');
$output = ob_get_contents();
ob_end_clean();
foreach(headers_list() as $header) {
    if(stripos($header, 'Location:') === 0){
        header_remove('Location');
        header($_SERVER['SERVER_PROTOCOL'] . ' 200 OK'); // Normally you do this
        header('Status: 200 OK');                        // For FastCGI use this instead
        header('X-Removed-Location:' . substr($header, 9));
    }
}
die('finished');

// HTTP/1.1 200 OK
// Server: Microsoft-IIS/5.1
// Date: Wed, 25 May 2011 11:57:36 GMT
// X-Powered-By: ASP.NET, PHP/5.3.5
// X-Removed-Location: http://www.google.com
// Content-Type: text/html
// Content-Length: 8

PS: , ob_start, PHP , ( script). . , .

+2

ob_start, :

. script ( ), .

0

, ,

:

, script ( )

: http://us.php.net/manual/en/function.ob-start.php

:

ob_start();
flush();
header("Location: http://www.google.com");
$output = ob_get_contents();
ob_end_clean();

$headers_sent = headers_sent();
$headers_list = headers_list();

var_dump($headers_sent);
var_dump($headers_list);

die('finished');
0
source

All Articles