Home > php教程 > php手册 > body text

PHP set_time_limit(0)长连接的实现分析

WBOY
Release: 2016-06-13 12:18:14
Original
993 people have browsed it

每个PHP脚本都限制了执行时间,所以我们需要通过 set_time_limit 来设置一个脚本的执行时间为无限长;然后使用 flush() 和 ob_flush() 来清除服务器缓冲区,随时输出脚本的返回值。

如下面这段脚本:

复制代码 代码如下:


header("Content-Type: text/plain");
set_time_limit(0);

$infoString = "Hello World" . "\n";
while( isset($infoString) )
{
echo $infoString;
flush();
ob_flush();
sleep(5);
}
?>


当我们执行后,每隔5秒钟,我们会得到一行 Hello World ,如果不按停止按钮,浏览器会不停的一行一行继续加载。

通过这一方法,我们可以完成很多功能,例如机器人爬虫、即时留言板等程序。
source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template