繁体   English   中英

PHP Header 即使在 output 缓冲区内也能发送位置?

[英]PHP Header Location gets sent even inside an output buffer?

我无法从 output 缓冲区内抑制 PHP 位置 Header。 据我了解,output 缓冲区应该抑制标头,直到它们被刷新。 我还认为不应使用 ob_end_clean() 发送任何标头。

但是,如果您看到下面的代码,如果我取消注释 header 行(第二行),我总是会被重定向到谷歌并且永远不会看到“完成”。

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');

我需要抑制任何 header 重定向,最好在 output 缓冲区中捕获它们,所以我知道这些条件会产生重定向。 我知道我可以用 curl 来做到这一点(将跟随重定向设置为 false),但由于我想要缓冲的所有文件都在我自己的服务器上 curl 被证明非常慢并且占用了大量的数据库连接。

有没有人有任何建议或知道任何捕获/抑制位置标题的方法?

谢谢,汤姆

看看你是否可以使用header_remove function 和headers_list 这似乎适用于 IIS/FastCGI 和 Apache:

<?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 将在即将发送 output 的第一个字节时(或脚本终止时)发送标头。 如果没有 output 缓冲,您的代码必须在发送任何 output 之前操作标头。 使用 output 缓冲,您可以随意交错 header 操作和 output 直到您刷新缓冲区。

如果您阅读ob_start的手册页,第一段是:

这个 function 将打开 output 缓冲。 虽然 output 缓冲处于活动状态,但不会从脚本发送 output (标题除外) ,而是将 output 存储在内部缓冲区中。

据我了解,output 缓冲区应该抑制标头,直到它们被刷新

没有:

虽然 output 缓冲处于活动状态,但不会从脚本发送 output(标题除外)

来源: 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');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM