PHP set_time_limit(0) long connection implementation code example

怪我咯
Release: 2023-03-12 19:38:02
Original
1037 people have browsed it

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 return value of the script at any time.

Such as the following script:

The code is as follows:

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

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

When we execute it, every 5 seconds, we will get a line of Hello World. If If you don't 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.

The above is the detailed content of PHP set_time_limit(0) long connection implementation code example. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!