Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial flush()函数无效?不能实时刷新输出,如何解决呢?

flush()函数无效?不能实时刷新输出,如何解决呢?

Jun 23, 2016 pm 02:38 PM

本帖最后由 zhoumengkang 于 2013-05-28 13:46:14 编辑

测试代码如下:
<?phpfor($i=0;$i<5;$i++){  	echo '<br/>abc___'.$i;	ob_flush();	flush();	sleep(1);}ob_end_flush();?> 
Copy after login

理论上应该是1秒出来一条是吧?但是我测试的结果是5秒之后才一起出来。
网上查了下有这样说的“个别web服务器程序,特别是Win32下的web服务器程序,在发送结果到浏览器之前,仍然会缓存脚本的输出,直到程序结束为止。 ”我这个是64位的windows的本地xampp的集成环境。


回复讨论(解决方案)

这种无端占用用户带宽的做法是很无聊的
延时输出的效果应在浏览器中用 js 实现

你的代码在 IE 中永远都不会看到期望的效果,因为 IE 需等待缓冲区装满后才解释收到的数据

这种无端占用用户带宽的做法是很无聊的
延时输出的效果应在浏览器中用 js 实现

你的代码在 IE 中永远都不会看到期望的效果,因为 IE 需等待缓冲区装满后才解释收到的数据
额,不是,我知识对这个函数不太了解。我用的chrome在测试。
其实我是想实现这样的效果:比如我写一个脚本批量处理一些文件
但那个过程可能很漫长,给别人用的话,别人都不知道是在执行呢,还是卡死了。
所以想每循环执行成功一次就输出一句xxxx执行成功;
-------------
最类似的就是咱们安装开源软件的时候见到的创建数据表的时候的显示。
但是我自己写的就会在数据表全部创建完之后才能显示。

这种无端占用用户带宽的做法是很无聊的
延时输出的效果应在浏览器中用 js 实现

你的代码在 IE 中永远都不会看到期望的效果,因为 IE 需等待缓冲区装满后才解释收到的数据
我的想法不是“延时”而是“即时”的显示我已完成的操作。不知道版主有什么高招。

同问!我现在也有一个程序要运行几个小时,一直运行,我只能去数据库查看数据来判断是否运行正常。
我的暂时做法只能做个简单的监控。比如在程序中加memcache写入操作,另外写个页面来读取。

同问!我现在也有一个程序要运行几个小时,一直运行,我只能去数据库查看数据来判断是否运行正常。
我的暂时做法只能做个简单的监控。比如在程序中加memcache写入操作,另外写个页面来读取。 有好的办法了,记得分享下 我去看看别的开源的代码在安装的时候是如何实现那个即时显示的效果的。

我又回来了,试试

<?phpecho str_pad('',4096);set_time_limit(50);for($i=0;$i<=5;$i++){    echo $i.'<br />';ob_flush();flush(); sleep(1);}?>
Copy after login
Copy after login
Copy after login

这两天出这个问题的还挺多的。
这个写法在各浏览器测试都没问题,但有童鞋说,这种做法往缓存区写垃圾,可以试试PHP抛出异常,有空研究一下再分享 。

我又回来了,试试

<?phpecho str_pad('',4096);set_time_limit(50);for($i=0;$i<=5;$i++){    echo $i.'<br />';ob_flush();flush(); sleep(1);}?>
Copy after login
Copy after login
Copy after login

这两天出这个问题的还挺多的。
这个写法在各浏览器测试都没问题,但有童鞋说,这种做法往缓存区写垃圾,可以试试PHP抛出异常,有空研究一下再分享 。
试了下,果然OK。机密是在哪里?

我又回来了,试试

<?phpecho str_pad('',4096);set_time_limit(50);for($i=0;$i<=5;$i++){    echo $i.'<br />';ob_flush();flush(); sleep(1);}?>
Copy after login
Copy after login
Copy after login

这两天出这个问题的还挺多的。
这个写法在各浏览器测试都没问题,但有童鞋说,这种做法往缓存区写垃圾,可以试试PHP抛出异常,有空研究一下再分享 。
完全是因为这个?echo str_pad('',4096);
第二个设置时限的可以省略,这个程序跑下来肯定不会超过30秒。
抛出异常,try-catch还可以这样用,学习了,我好好想想!你是一个好人!

嗯嗯,我的这个程序一开始考虑用php_cli,因为程序步骤多,觉得不友好,还是选择页面;但运行时间太长,开着页面反倒成了累赘,还是用cli吧,加油。

嗯嗯,我的这个程序一开始考虑用php_cli,因为程序步骤多,觉得不友好,还是选择页面;但运行时间太长,开着页面反倒成了累赘,还是用cli吧,加油。 今天又试了下用chrome测试,还是等运算完了,一次性输出。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

See all articles