PHP in , A request is issued on the client to trigger the execution of the script , Then execute a piece of code on the server side , If the page is closed, continue to execute , And to return some status to the client first , Avoid front end wait timeout .
/**************** 1 Action before returning to the front-end result ****************/ /**************** 2
Tell front end result , Tell the browser that the operation was successful , But keep running php ****************/ ob_end_clean(); ob_start();
//Windows The server needs to add this line . echo str_repeat(" ",4096);//windows // echo str_repeat(' ',
65536); //linux
//-----------------------------------------------------------------------------------
_ajaxReturntwo(200,' The operation was successful ');// Return results to ajax
//-----------------------------------------------------------------------------------
$size = ob_get_length(); header("Content-Length: ".$size); header("Connection:
close"); header("HTTP/1.1 200 OK"); header('Content-Type:application/json;
charset=utf-8'); ob_end_flush(); if(ob_get_length()) ob_flush(); flush(); if
(function_exists("fastcgi_finish_request")) { //
yii or yaf The default is not output immediately , Add this sentence ( The premise is to use fpm) fastcgi_finish_request(); // Response complete ,
Go back to the front end immediately , Close connection } ignore_user_abort(true);// After closing the connection , Keep running php script set_time_limit(0);
//no time limit, Do not set the timeout ( Use according to the actual situation ) /**************** 2 Tell front end result , Tell browser to finish , But keep running php
****************/ /**************** 3 Keep running php ****************/ sleep(20); /*
dormancy PHP, That is, the current situation PHP Code execution stopped ,20 Seconds later PHP Awakened , PHP After awakening , Continue with the following code , But by this time, the results of the above code have been output to the browser ,
That is, the browser from the HTTP The header knows that the server has closed the connection , The browser will not be waiting for a response from the server ,
The response to the customer is that the page will not appear to be loaded , In other words, the user can turn off the current page , Or turn off the browser , PHP After waking up, continue to execute the following code , And that's what happened PHP Effect of background execution ,
The function of dormancy is to make php Finish the previous output first , Don't rush to execute the following code immediately , Just a break , That is to say, the following code When executing, the previous output should reach the browser */
....... Continue to write the logic of the function function _ajaxReturntwo($code, $msg, $data=array()) { $data =
is_null($data) ? array() : $data; $data = array( "code" => $code, "msg" =>
$msg, "data" => json_encode($data) );
header('Cache-Control:no-cache,must-revalidate'); header('Pragma:no-cache');
header('Content-Type:application/json; charset=utf-8');
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers:x-requested-with,content-type"); echo
json_encode($data); }
 

Technology