PHP timed execution and use of Ob_flush () and flush ()

Source: Internet
Author: User
Tags php script

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

http://blog.csdn.net/qq_38125058

One: Executes every 30s, writes a string to the file

// 30秒执行一次    ignore_user_abort(true); //即使Client断开(如关掉浏览器),PHP脚本也可以继续执行.      set_time_limit(0); // 执行时间为无限制,php默认执行时间是30秒,可以让程序无限制的执行下去 $interval=30; //每隔30秒运行一次 do{ /** **要执行的操作 */ // file_put_contents("D://log.log",123,FILE_APPEND);//记录日志 sleep($interval); // 按设置的时间等待30秒循环执行 // 其他操作 }while(true);

Note: file_put_contents ("D://log.log", $msg, file_append); The function of the method is to write a string to the file. file_append effect: Append data instead of overwrite if the file already exists.
Execute the above code to see a string of "123" written in the Log.log file every 30s.

Second: Every 30s in the page display string

// 30秒执行一次ignore_user_abort(); //即使Client断开(如关掉浏览器),PHP脚本也可以继续执行.  set_time_limit(0); // 执行时间为无限制,php默认执行时间是30秒,可以让程序无限制的执行下去  $interval=30; // 每隔30秒运行一次  do{      // 要执行的操作      echo "string";    ob_flush();//把数据从PHP的缓冲(buffer)中释放出来。    flush(); //把不在缓冲(buffer)中的或者说是被释放出来的数据发送到浏览器。    sleep($interval); // 按设置的时间等待30秒循环执行          // 其他操作 }while(true);

The flush () function does not affect the caching mode of the server or client browser. Therefore, you must use both the Ob_flush () and flush () functions to flush the output buffers. The order is first Ob_flush (), then Flush (), and their function is to flush the buffer.

Buffer is a memory address space, the Linux system default size is generally 4096 (1KB), that is, a memory page. It is primarily used to store data between devices that are out of sync or between devices with different priority levels. By using buffer, you can make the process less of a mutual wait.
When executing the echo,print, the output is not immediately displayed via TCP to the client browser, but instead writes the data to PHP buffer. The PHP output_buffering mechanism means that a new queue is established before the TCP buffer, and the data must pass through the queue. When a PHP buffer is full, the script process will send the output data from PHP buffer to the system kernel to be displayed by TCP to the browser. So, the data will be written to these places echo/pring. PHP Buffer---browser

By default, PHP buffer is turned on, and the default value of this buffer is 4096, which is 1kb. You can find the output_buffering configuration in the php.ini configuration file. The output data is written to the PHP output_buffering when the user data is Echo,print, until Output_ Buffering is full, this data will be transmitted via TCP to the browser display. You can also manually activate the PHP output_buffering mechanism through Ob_start (), so that even if the output exceeds 1KB data, it does not really give the data to TCP to the browser, because Ob_start () set the PHP buffer space to large enough. Data is sent to the client browser only until the script finishes, or when the Ob_end_flush function is called.

When the cache is not turned on, the contents of the script output are in the state of waiting for output on the server side, and flush () can send the content waiting for output to the client immediately.
When the cache is turned on, the contents of the script output are stored in the output cache, and there is no content waiting for the output state, and you directly use flush () without sending anything to the client. The function of Ob_flush () is to remove the contents of the output cache, set to wait for the output state, but not directly to the client, you need to use Ob_flush () and then flush () before the client can immediately get the output of the script.

The code program of the above timed operation does not have a write-off judgment statement, and he will loop indefinitely.

The following are the improved solutions:

Three: 30 seconds after the output string "123", loop stop.

    //30 seconds after the execution of Ignore_user_abort ();      //even if the client disconnects (such as turning off the browser), the PHP script can continue to execute. Set_time_limit (0); //execution time is unlimited, php default execution time is 30 seconds, you can allow the program to carry out indefinitely  $interval =30; //run once every 30 seconds  $status = 1; while ( $status) { $status = 0; Sleep ( $interval); echo  "123"; Ob_flush (); //sends the data that is not in buffer or is released to the browser. } exit ();             

Personal feeling PHP is not very efficient to perform tasks on a regular basis, so it is not recommended.

PHP timed execution and use of Ob_flush () and flush ()

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.