Home  >  Article  >  Backend Development  >  PHP set_time_limit(0) function to implement long connection explanation_PHP tutorial

PHP set_time_limit(0) function to implement long connection explanation_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:40:42901browse

Every time we access a PHP script, we only get the return result after all PHP scripts have been executed. If we need a script to run continuously, then we have to use PHP long connection to achieve the purpose of running.

Each PHP script has a limited execution time, so we need to set the execution time of a script to unlimited through set_time_limit; then use flush() and ob_flush() to clear the server buffer and output the script at any time return value.

Such as the following script:
header("Content-Type: text/plain");
set_time_limit(0);

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

When we execute, we will get a line of Hello World every 5 seconds. If we do not press the stop button, the browser will continue to load line by line
.
Through this method, we can complete many functions, such as robot crawlers, instant message boards and other programs

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486210.htmlTechArticleEvery time we access a PHP script, we only get the return after all PHP scripts have been executed. result. If we need a script to run continuously, then we have to pass...
Statement:
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