php浏览器上实时输出内容

参考代码

 

Php代码  收藏代码
  1. <?php  
  2. ini_set("display_errors", "On");  
  3.   
  4. /*输出内容函数*/  
  5. global $echo_msg_head;  
  6. $echo_msg_head = null;  
  7. error_reporting(E_ALL);  
  8. for( $i=1; $i<=10; $i++ ){  
  9.     echo_msg('第 '.$i.' 次输出https://dev.xiaoshanwenlv.yocent.cn/test.php https://dev.xiaoshanwenlv.yocent.cn/test.php.');  
  10.   
  11.     sleep(1);  
  12.   
  13. }  
  14. function echo_msg($str, $color = 'red', $echo = true, $end_str = '')  
  15. {  
  16.     if ($echo) {  
  17.         if (!isset($GLOBALS['echo_msg_head'])) {  
  18.             $GLOBALS['echo_msg_head']=true;  
  19.             header('Content-type: text/html; charset=utf-8');  
  20.             header('X-Accel-Buffering: no');  
  21.             ini_set('output_buffering', 'Off');  
  22.   
  23.             ob_end_clean();  
  24.             ob_implicit_flush(true);  
  25.   
  26.             $outputSize=ini_get('output_buffering');  
  27.             $outputSize=intval($outputSize);  
  28.   
  29.             if (preg_match('/\biis\b/i', $_SERVER["SERVER_SOFTWARE"])) {  
  30.                 if ($outputSize<1024*1024*4) {  
  31.                     $outputSize=1024*1024*4;  
  32.                     echo '<!-- iis默认需输出4mb数据才能实时显示-->';  
  33.                 }  
  34.             }  
  35. //            var_dump($outputSize>1024*1024);  
  36.             echo '<style type="text/css">body{padding:0 5px;font-size:14px;color:#000;}p{padding:0;margin:0;}a{color:#aaa;}</style>';  
  37.   
  38.             $allowOutput=true;  
  39.             if ($allowOutput) {  
  40.                 echo str_pad(' ', $outputSize>1050?($outputSize+100):1050);  
  41.             }  
  42.         }  
  43.         echo '<p style="color:'.$color.';">'.$str.'</p>'.$end_str;  
  44.         if (ob_get_level()>0) {  
  45.             ob_flush();  
  46.             flush();  
  47.         }  
  48.     }  
  49. }  
  50. //  
  51. //header('Content-Type: text/event-stream'); // 以事件流的形式告知浏览器进行显示  
  52. //header('Cache-Control: no-cache');         // 告知浏览器不进行缓存  
  53. //header('X-Accel-Buffering: no');           // 关闭加速缓冲  
  54. //  
  55. //$buffer = ini_get('output_buffering');  
  56. //$outputSize=intval($buffer);  
  57. //for($i=0;$i<10;$i++){  
  58. //    echo str_repeat('1',$outputSize).$i;  
  59. //    sleep(1);  
  60. //    ob_flush();  
  61. //    flush();  
  62. //}  
  63.   
  64. //  
  65. //$buffer = ini_get('output_buffering');  
  66. //echo $buffer;  
  67. //  
  68. //echo str_repeat(' ',$buffer+1);     //防止浏览器缓存  
  69. //ob_end_clean();  
  70. //ob_implicit_flush(1);  
  71. ////ob_end_flush();     //关闭缓存  
  72. //  
  73. //for( $i=1; $i<=10; $i++ ){  
  74. //  
  75. //    echo '第 '.$i.' 次输出.'."  
  76. //\n";  
  77. //  
  78. //    ob_end_flush();  
  79. //    ob_flush();  
  80. //    flush();  
  81. //  
  82. //    sleep(1);  
  83. //  
  84. //}  
  85.   
  86. echo '输出完毕!';  

 

 

如果nginx转发过来的网站,在转发配置里,要关闭 proxy_buffering off;

 

Php代码  收藏代码
  1. server  
  2.     {  
  3.         listen 443 ssl;  
  4.         #listen [::]:80;  
  5.         server_name dev.xiaoshanwenlv.yocent.cn;  
  6.         index index.html index.htm index.php default.html default.htm default.php;  
  7.   
  8.     ssl on;  
  9.     ssl_certificate   ../cert/4030331_dev.xiaoshanwenlv.yocent.cn.pem;  
  10.     ssl_certificate_key  ../cert/4030331_dev.xiaoshanwenlv.yocent.cn.key;  
  11.   
  12.         ssl_session_timeout 5m;  
  13.         ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;  
  14.         ssl_protocols TLSv1 TLSv1.1 TLSv1.2;  
  15.         ssl_prefer_server_ciphers on;  
  16.   
  17.   
  18.         location / {  
  19.         proxy_set_header Host $host;  
  20.         proxy_set_header Server-Port 443;  
  21.         proxy_set_header X-Real-IP $remote_addr;  
  22.         proxy_set_header X-Forwarded-Server $host;  
  23.         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
  24.         proxy_buffering off;  
  25.         proxy_pass http://dev.xiaoshanwenlv.yocent.cn:8888;  
  26.     }  
  27.   
  28.          access_log  /home/wwwlogs/dev.xiaoshanwenlv.yocent.cn.log  access;  
  29.     }  
posted @ 2021-01-28 18:38  经管时评  阅读(146)  评论(0编辑  收藏  举报