PHP Output Control controll (header, ob_xxx)

Source: Internet
Author: User

About the output controll reference documentation for PHP:

Http://gywbd.github.io/posts/2015/1/php-output-buffer-in-deep.html

Http://www.cnblogs.com/liuzhang/p/4161213.html

It is well known that there can be no output to the browser before calling the header () function, otherwise it will be an error.

How to reproduce the situation:

1, php.ini set output_buffering OFF

2. Test code:

2 ; header ("http/1.1 503 Service unavailable");

3, this procedure will be error:

Warning:cannot Modify header Information-headers already sent by

How to fix this error:

Method 1:

PHP.ini output_buffering 4096 sets a 4K size so that the echo of the program is cached to PHP (or cached to Web server?) until the cache size is 4K or the Ob_ series function is called.

This is why the following code will not show all results until sleep 2s:

2 ; Sleep (23;

Method 2:

In Method 1, output_buffering 4096 means that the default PHP program turns on the output (for functions such as echo printf) to be cached (the effect is equivalent to the Ob_start () function), at which point the call to Ob_get_level () results in 1.

So you can call close cache function to close the output cache

Ob_end_clean () or Ob_get_clean ()

2 ; header ("http/1.1 503 Service unavailable");

This will be an error.

But the following code does not immediately output the result:

2 ; Sleep (23;

The reason for this is that the Ob_ function only shuts down the output cache of PHP, and the Echo is still cached in the Web server, and only the Web server waits for the program to complete sending the output to the browser.

If you want the program to echo a thing the browser displays one, you can do the following:

Echo Str_pad (",4096). " \ n " ;   1 ; Ob_flush (); flush (); Sleep (22;

@todo not finished, need to be re-rationale, not fully understand. Some of the flush and OB series functions are controlled by the PHP cache, and some are controlled by the Web server cache.

PHP Output Control controll (header, ob_xxx)

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.