echo Output analysis before the header () function of PHP

Source: Internet
Author: User

The header () method of PHP is generally used to tell the browser what to do, such as jump, refresh, and so on, and before calling the header method, what happens if you use echo to output some content? Does it affect the header function? We experiment to see the situation.

Part I: Choosing an environment and version

Our test selection is that the lnnp,php version is 5.3.8, the relatively older version

  

Part II: Test Cases

<?PHP$str= "qwertyuiopasdfghjklzxcvbnm1234567890~!@#$%^&* () _+";$time= 10;  for($i= 0;$i<$time;$i++){  $x=Rand(0, 40); $v=substr($str,$x, 1); Echo $v;}Header(' Refresh:3, http://www.baidu.com ');

Explain this test case,

1. $time variable to control the size of the echo output character

2. Why do I use a For loop to output random characters? Reason from the third part of the case to grasp the package can be seen, the browser side generally requires response content part of gzip compression. And this compression is actually done by PHP, in order to exclude gzip compression effect on the experiment, so with a for loop to do the disorderly output

Part III: Packet Capture analysis

1, the normal situation, $time variable relatively small case

1) The browser sends the request to Nginx

 

  

2) Nginx will request packaging and send to PHP-FPM

  

3) php-fpm the results back to Nginx

4) Nginx will return the result to the browser

 

2, abnormal situation, $time variable in the case of large
We look at the third step in 1, php-fpm the results back to Nginx, we found two points
1) Unable to implement jump. From the case of the packet capture is PHP can not modify the output header information, the reason should be header information, has been write to the socket FD, the cache pool does not exist header information, so cannot modify
2) The content of the echo output has no effect.

Part IV: Analysis and conclusions

Combined with the above, from the third part of the 1th, we can actually see that the implementation of the header function, is actually the header function parameters, as a string added to the output of the header of the return content, and when the echo content is large, The header string cannot be exported.

We can see that the process of PHP processing echo will be placed in the buffer pool, when the cache pool full, the contents of the cache pool will be write to the socket FD, once write in, The header function will no longer be able to write the header string to the header section of the return request. So in this case, the header function will fail.

echo Output analysis before the header () function of PHP

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.