简体   繁体   English

为什么我没有收到“警告:无法修改标头信息-标头已经发送”的信息,我希望得到它?

[英]Why I'm not getting a 'Warning: Cannot modify header information - headers already sent by' as I expected to get one?

I'm using PHP 7.1.11 on a machine that runs on Windows 10 Home Single Language edition. 我在Windows 10家庭单一语言版本上运行的计算机上使用PHP 7.1.11

I'm using XAMPP server on my machine. 我在机器上使用XAMPP服务器。

I'm using following browsers on this machine : 我在这台机器上使用以下浏览器:

  1. Google Chrome(Version 62.0.3202.94 (Official Build) (64-bit)) Google Chrome(版本62.0.3202.94(正式版本)(64位))
  2. Firefox Quantum(57.0.1 (64-bit)) Firefox Quantum(57.0.1(64位))
  3. Opera(Version : 49.0.2725.47) Opera(版本:49.0.2725.47)
  4. Microsoft Edge 41.16299.15.0 微软Edge 41.16299.15.0

I know the details of header() function and how it works. 我知道header()函数的详细信息及其工作方式。

But following program is behaving in really a weird way on all of the above four web browsers. 但是,在上述四种Web浏览器上,以下程序的运行方式确实很怪异。 Even after sending output to the client the header() function is working. 即使在将输出发送到客户端之后,header()函数仍在工作。

How can this be possible? 这怎么可能?

Below is my code(It's getting redirected to the URL I mentioned) : 以下是我的代码(将其重定向到我提到的URL):

    <!DOCTYPE html>
    <html>
      <body>
        <p>Welcome to my website!</p><br /> 
        <?php
          ini_set('display_errors', 1);
          ini_set('display_startup_errors', 1);
          error_reporting(E_ALL); 
          if($test) { 
            echo 'Welcome to my website<br />You\'re in!'; 
          } else { 
            header('Location: http://www.espncricinfo.com'); 
          } 
        ?>
      </body>
    </html>

I was expecting to get the warning 'Cannot modify header information - headers already sent by' but surprisingly it's getting redirected me to the URL http://www.espncricinfo.com/ ? 我原本希望收到警告“无法修改标头信息-标头已发送”,但令人惊讶的是它将我重定向到了URL http://www.espncricinfo.com/

Why? 为什么?

If you are somehow using output buffering — either manually , or because your PHP is configured to automatically do this — you are still allowed to add headers, so long as the initial buffer has not been flushed. 如果您以某种方式使用输出缓冲 (无论是手动操作还是由于PHP配置为自动执行此操作),只要未刷新初始缓冲区,仍然可以添加标头。

What output buffering does is what the name hints at: put output in a buffer to be sent at a later stage, in stead of immediately outputting data. 名称所暗示的是输出缓冲的作用:将输出放在缓冲区中,以便稍后发送,而不是立即输出数据。 Because of this, so long as no body data of the HTTP response message has been sent, you are still able to send header data. 因此,只要未发送HTTP响应消息的正文数据,您仍然可以发送标头数据。


To check whether PHP is configured to automatically buffer output, you can do one of the following: 要检查PHP是否配置为自动缓冲输出,可以执行以下操作之一:

  1. check the php.ini configuration file and search for the string output_buffering 检查php.ini配置文件并搜索字符串output_buffering
  2. var_dump( ini_get( 'output_buffering' ) );
  3. phpinfo(); (dumps the whole configuration) and search for the string output_buffering (转储整个配置)并搜索字符串output_buffering

If output buffering is enabled, the value will either be the buffer size in bytes (if configured as such), or "On" or true , or something to that effect. 如果启用了输出缓冲,则该值将是缓冲区大小(以字节为单位)(如果已如此配置), "On"true或与此相关的值。

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

相关问题 为什么收到警告:无法修改标头信息-标头已经发送过(输出从...开始)? - Why am I getting Warning: Cannot modify header information - headers already sent by (output started at …)? 警告:无法修改标题信息 - 标题已在 wordpress 中发送 - Warning: Cannot modify header information - headers already sent by in wordpress 如何删除警告无法修改标头信息-标头已发送 - How to remove warning Cannot modify header information - headers already sent by 警告:无法修改标头信息-标头已发送 - Warning: Cannot modify header information - headers already sent by 无法修改标题信息-标题已发送 - Cannot modify Header Information - headers already sent 无法修改标头信息 - 标头已发送 - Cannot modify header information - headers already sent 无法修改标头信息-标头已由发送 - Cannot modify header information - headers already sent by 无法修改已发送的标头信息标头 - Cannot Modify Header Information Headers Already Sent 关于警告的奇怪错误:无法修改标题信息-没有空格PHP时已经发送的标题 - Strange error regarding Warning: Cannot modify header information - headers already sent by when there is no white space PHP Wordpress提要:PHP警告无法修改标题信息-标题已发送 - Wordpress Feeds: PHP Warning Cannot modify header information - headers already sent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM