Implementation of long Connection in PHP

Source: Internet
Author: User
Recently encountered PHP program in the execution of large amounts of data when prompted to time out, so the use of the Set_time_limit () function to set the maximum PHP page run time.
Sets the number of seconds that the script is allowed to run. If this is the default, the script returns a fatal error. The default limit is 30 seconds, or you can set the maximum execution time for PHP pages in php.ini definition max_execution_time.
When called, the Set_time_limit () function restarts the timeout counter from zero. In other words, if the default time-out is 30 seconds, 25 seconds to the execution parameters of the script, or set_time_limit, such as setting the timeout to (20), the script will run for 45 seconds before it times out.
Set_time_limit (900) This function specifies the maximum execution time of the current PHP script, although the set value is 900 seconds, in fact the maximum execution time =php.ini in the Max_execution_time value-the current script has been executed time + set value.
If max_execution_time=30 in php.ini, the current script has been executed for 10 seconds, then:
Maximum execution time =30-10+900=920 seconds.
After this modification, the PHP script successfully updated 200,000 records.
Every time we access the PHP script, we get back the results when all the PHP scripts have been executed. If we need a script to run continuously, then we have to use the long connection of PHP to achieve the purpose of running.

Each PHP script limits the execution time, so we need to set a script to execute indefinitely with Set_time_limit, and then use Flush () and Ob_flush () to clear the server buffer and output the return value of the script at any time.

 
  

When we do, every 5 seconds, we get a row of Hello world, and if you don't press the Stop button, the browser will continue to load on one line.
Through this method, we can complete many functions, such as robot Crawler, instant message board and other programs.

The above introduces the implementation of the long connection in PHP, including the content, I hope the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.