简体   繁体   中英

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.

I'm using XAMPP server on my machine.

I'm using following browsers on this machine :

  1. Google Chrome(Version 62.0.3202.94 (Official Build) (64-bit))
  2. Firefox Quantum(57.0.1 (64-bit))
  3. Opera(Version : 49.0.2725.47)
  4. Microsoft Edge 41.16299.15.0

I know the details of header() function and how it works.

But following program is behaving in really a weird way on all of the above four web browsers. Even after sending output to the client the header() function is working.

How can this be possible?

Below is my code(It's getting redirected to the URL I mentioned) :

    <!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/ ?

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.

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.


To check whether PHP is configured to automatically buffer output, you can do one of the following:

  1. check the php.ini configuration file and search for the string output_buffering
  2. var_dump( ini_get( 'output_buffering' ) );
  3. phpinfo(); (dumps the whole configuration) and search for the string 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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