The secret between the PHP buffer and the header function

Source: Internet
Author: User
In the actual development, have we heard that there is no actual output before the header. Even some think that the header function must be written at the front of the code. But have you ever tried to output something before the header function? Let's get a deeper look at it.

    • Test header has output before
         
          I was tested to be successful without any errors or warnings. Don't know what you guys are like? But I think most say is no problem, if there is cannot modify header information-headers already sent such a warning, this is to say can not modify header information, header information has been sent. Come down and find out why there are two different outcomes?

Buffer

As a metaphor, it's like a cache when we watch a movie. It will not be played to us immediately, but the first part of the download of a good movie in the cache, and then the cache play out. And that's how we write PHP code.

PHP's caching mechanism-output_buffering

    • Common OB functions in PHP
function explain
Ob_start Open output Buffers
Ob_clean emptying buffers
Ob_get_contents Return buffer contents
Ob_get_clean Returns the contents of the buffer and empties
    
 
 
    • In the php.ini configuration file, modify the buffer size

Usually around 233 rows, the default is 4096 means 4096 bytes is 4kB

Down 4096 to 5, rerun this code

     

There are no warnings or errors for the test just now, but the error is:cannot modify header information-headers already sent

Analysis between header and buffer

    • Why can't we have output before we say header?

      For the header function, it is like the client sends the original HTTP header, is to declare what we are writing the page exactly what content, so one but this statement before the content is wrong, is not compliant with the HTTP rules

    • Come down and talk about the header in PHP.

      In PHP, the header is not through the buffer, it will go through the server output directly to the client

    • Explanation of previous warning cannot modify header information

      When we write some output before the header, it passes through the buffer first. So even if you write the front, the final output sequence is the first header in Echo.

      But the content buffers we output are not fit, that is, the previous output ' Hello world! ' > 5 bytes. will be output directly, that is, the first output ' Hello World ' again header (...), so that it violates the real header can not have output

Summarize

In practice, we'd better write the header at the front of the page. Because we are not sure if the output before our header is buffer can be dropped.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.