Heim  >  Artikel  >  Backend-Entwicklung  >  PHP set_time_limit(0)长连接的实现分析_php技巧

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

WBOY
WBOYOriginal
2016-05-17 09:27:35881Durchsuche

每个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 ,如果不按停止按钮,浏览器会不停的一行一行继续加载。

通过这一方法,我们可以完成很多功能,例如机器人爬虫、即时留言板等程序。
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn