PHP Background execution

Source: Internet
Author: User

Ways to implement background execution in PHP:

Ignore_user_abort (TRUE); Running in the background
Set_time_limit (0); Maximum timeout for canceling script run time
Behind the scenes to run, Set_time_limit (0); Unless you close the program on the server, the following code will be executed forever until it is complete.
If the program does not run out of time, the program does not end automatically until the execution finishes. =========================================

How PHP triggers on the client and then executes a function on the server side, and the page close also continues. To first return the user request not to wait.

Ob_end_clean (); #清除之前的缓冲内容, this is required, if the previous cache is not empty, there may be HTTP headers or other content, resulting in the subsequent content can not be output in a timely manner
Header ("Connection:close");//Tell the browser that the connection is off so that the browser does not have to wait for the server to respond
Header ("http/1.1 OK"); 200 status codes can be sent to these requests to be successful, otherwise the browser may retry, especially if there is an agent
Return false;//added this below will not execute, do not add this cannot return the page state, the browser has been waiting for the state, can be closed, but not to the effect.
Die (); or return; Do not perform the following
Rundata ();
Register_shutdown_function ("Rundata");
return;
Ob_start (); #开始当前代码缓冲
echo "Running,,,,.";
The following output some header information for HTTP
$size =ob_get_length ();
Header ("Content-length: $size");
Ob_end_flush (); #输出当前缓冲
Flush (); #输出PHP缓冲

#休眠PHP, that is, the execution of the current PHP code is stopped, 1 seconds after PHP was awakened,
#PHP唤醒后, proceed to the following code, but this time the result of the above code has been output browser,
#也就是浏览器从HTTP头中知道了服务端关闭了连接, the browser will not be waiting for a response from the server,
#反应给客户的就是页面不会显示处于加载状态中, in other words, users can switch off the current page, or switch off the browser,
#PHP唤醒后继续执行下面的代码, this also implements the effect of PHP background execution,
#休眠的作用只是让php先把前面的输出作完, do not rush to execute the following code immediately, take a break, that is to say the following code
#执行的时候前面的输出应该到达浏览器了
Sleep (1);
Echo ' Here the output user does not see, the background runs ';

Any output from the following code will not be output to the browser, because the HTTP connection is closed.
So the execution of the following code belongs to the background run

Ignore_user_abort (TRUE); Run in the background, this just runs the browser close, not directly on the abort return 200 status.
Set_time_limit (0); Maximum timeout for canceling script run time
Rundata ();

function Rundata () {//do something}

========================================= using Ignore_user_abort function to implement PHP planning tasks
The code is as follows:
<?php
Ignore_user_abort (TRUE);
Set_time_limit (0);
while (1) {
$fp = fopen (' Time_task.txt ', "A +");
$STR = Date ("y-m-d h:i:s"). " \n\r ";
Fwrite ($fp, $STR);
Fclose ($FP);
Sleep (300); Execute once in half an hour
}
?> =======================================int Ignore_user_abort ([bool setting])
The purpose of this function is to indicate whether the server side continues to execute the following script after the remote client closes the connection.
The setting parameter is an optional parameter. If set to True, indicates that the script will not be affected if the user stops running the script (that is, the script continues to execute), and if set to false means that the script will stop running when the user stops running the script.
In the following example, after the user closes the browser, the script continues to execute on the server:
Ignore_user_abort (TRUE); Running in the background
Set_time_limit (0); Maximum timeout for canceling script run time
do{
Sleep (60); Sleep for 1 minutes
}while (TRUE);
?>
This code will be executed forever unless the program is closed on the server.
-------------------------------------------------------------------------
Ignore_user_abort (TRUE); Running in the background
Set_time_limit (0); Maximum timeout for canceling script run time
Echo ' start. ';
Sleep (1000);
Echo ' End. ';
?>

PHP Background execution

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.