0

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?

PHPLover
  • 1
  • 51
  • 158
  • 311
  • 1
    are you getting a "notice"? sounds like your error checking is disabled –  Dec 04 '17 at 05:08
  • Check your PHP configuration to see if you have output buffering enabled. – Decent Dabbler Dec 04 '17 at 05:08
  • 1
    @nogad : I'm not getting any notice or anything else. When I hit the URL of this file from my localhost I'm getting redirected to the URL http://www.espncricinfo.com. My question is irrespective of error reporting status being on or off the header() function should not have to work as some output has already been sent to the browser. Why this is happening? – PHPLover Dec 04 '17 at 05:17
  • you should get a notice for $test, so your error checking\display are off –  Dec 04 '17 at 05:18
  • @DecentDabbler : What output buffering has to do here? – PHPLover Dec 04 '17 at 05:19
  • @nogad No, even with with error reporting disabled, the header would not be sent. – Decent Dabbler Dec 04 '17 at 05:19
  • I'll explain in an answer. – Decent Dabbler Dec 04 '17 at 05:19
  • @nogad : Please check the updated code with error reporting turned on but still the output is same. I'm getting redirected and no notice or error is being issued. – PHPLover Dec 04 '17 at 05:22
  • @DecentDabbler : Please check the updated code with error reporting turned on but still the output is same. I'm getting redirected and no notice or error is being issued. – PHPLover Dec 04 '17 at 05:23
  • Yep, error reporting is irrelevant to this issue. – Decent Dabbler Dec 04 '17 at 05:53

1 Answers1

1

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.

Decent Dabbler
  • 22,532
  • 8
  • 74
  • 106
  • How should I check the status of output buffering in above code? mean whether it's turned on or off in configuration. – PHPLover Dec 04 '17 at 05:29
  • @PHPLover See my addition to my answer. – Decent Dabbler Dec 04 '17 at 05:43
  • I checked the status buffering status using var_dump( ini_get( 'output_buffering' ) ); die; by righting this code line immediately after PHP opening tag – PHPLover Dec 04 '17 at 06:06
  • So, can I say output buffering is truned on in configurations and output buffering is the only cause for executing header() function in my program? – PHPLover Dec 04 '17 at 06:07
  • 1
    @PHPLover Yep, if you have access to the `php.ini` configuration file, set `output_buffering = Off` (search for `output_buffering`) and restart your web server. Or, alternatively, output more than 4096 bytes of body data first and try to call `header()` then. You should now get the warning you expected again. – Decent Dabbler Dec 04 '17 at 06:20