php浏览器上实时输出内容
参考代码
- <?php
- ini_set("display_errors", "On");
- /*输出内容函数*/
- global $echo_msg_head;
- $echo_msg_head = null;
- error_reporting(E_ALL);
- for( $i=1; $i<=10; $i++ ){
- echo_msg('第 '.$i.' 次输出https://dev.xiaoshanwenlv.yocent.cn/test.php https://dev.xiaoshanwenlv.yocent.cn/test.php.');
- sleep(1);
- }
- function echo_msg($str, $color = 'red', $echo = true, $end_str = '')
- {
- if ($echo) {
- if (!isset($GLOBALS['echo_msg_head'])) {
- $GLOBALS['echo_msg_head']=true;
- header('Content-type: text/html; charset=utf-8');
- header('X-Accel-Buffering: no');
- ini_set('output_buffering', 'Off');
- ob_end_clean();
- ob_implicit_flush(true);
- $outputSize=ini_get('output_buffering');
- $outputSize=intval($outputSize);
- if (preg_match('/\biis\b/i', $_SERVER["SERVER_SOFTWARE"])) {
- if ($outputSize<1024*1024*4) {
- $outputSize=1024*1024*4;
- echo '<!-- iis默认需输出4mb数据才能实时显示-->';
- }
- }
- // var_dump($outputSize>1024*1024);
- echo '<style type="text/css">body{padding:0 5px;font-size:14px;color:#000;}p{padding:0;margin:0;}a{color:#aaa;}</style>';
- $allowOutput=true;
- if ($allowOutput) {
- echo str_pad(' ', $outputSize>1050?($outputSize+100):1050);
- }
- }
- echo '<p style="color:'.$color.';">'.$str.'</p>'.$end_str;
- if (ob_get_level()>0) {
- ob_flush();
- flush();
- }
- }
- }
- //
- //header('Content-Type: text/event-stream'); // 以事件流的形式告知浏览器进行显示
- //header('Cache-Control: no-cache'); // 告知浏览器不进行缓存
- //header('X-Accel-Buffering: no'); // 关闭加速缓冲
- //
- //$buffer = ini_get('output_buffering');
- //$outputSize=intval($buffer);
- //for($i=0;$i<10;$i++){
- // echo str_repeat('1',$outputSize).$i;
- // sleep(1);
- // ob_flush();
- // flush();
- //}
- //
- //$buffer = ini_get('output_buffering');
- //echo $buffer;
- //
- //echo str_repeat(' ',$buffer+1); //防止浏览器缓存
- //ob_end_clean();
- //ob_implicit_flush(1);
- ////ob_end_flush(); //关闭缓存
- //
- //for( $i=1; $i<=10; $i++ ){
- //
- // echo '第 '.$i.' 次输出.'."
- //\n";
- //
- // ob_end_flush();
- // ob_flush();
- // flush();
- //
- // sleep(1);
- //
- //}
- echo '输出完毕!';
如果nginx转发过来的网站,在转发配置里,要关闭 proxy_buffering off;
- server
- {
- listen 443 ssl;
- #listen [::]:80;
- server_name dev.xiaoshanwenlv.yocent.cn;
- index index.html index.htm index.php default.html default.htm default.php;
- ssl on;
- ssl_certificate ../cert/4030331_dev.xiaoshanwenlv.yocent.cn.pem;
- ssl_certificate_key ../cert/4030331_dev.xiaoshanwenlv.yocent.cn.key;
- ssl_session_timeout 5m;
- ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
- ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
- ssl_prefer_server_ciphers on;
- location / {
- proxy_set_header Host $host;
- proxy_set_header Server-Port 443;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-Server $host;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_buffering off;
- proxy_pass http://dev.xiaoshanwenlv.yocent.cn:8888;
- }
- access_log /home/wwwlogs/dev.xiaoshanwenlv.yocent.cn.log access;
- }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端