I have two PHP scripts—we will call them script A and script B. Script A starts running when the user does a certain interaction. Script A needs to run script B in the background—thus return script A's result while script B is still running. I could do this inside script A: exec('scriptB.php &')
, however since I'm on shared hosting exec
is not allowed. Also, an Ajax solution (initiate both scripts on the client-side) will not work since script B MUST run—I can't have the user maliciously stopping the script from running.
Is there any solution that does not involve using a shell command or Ajax?
Thanks in advance!
I ended up using this method: http://w-shadow.com/blog/2007/10/16/how-to-run-a-php-script-in-the-background/
function backgroundPost($url){
$parts=parse_url($url);
$fp = fsockopen($parts['host'],
isset($parts['port'])?$parts['port']:80,
$errno, $errstr, 30);
if (!$fp) {
return false;
} else {
$out = "POST ".$parts['path']." HTTP/1.1\r\n";
$out.= "Host: ".$parts['host']."\r\n";
$out.= "Content-Type: application/x-www-form-urlencoded\r\n";
$out.= "Content-Length: ".strlen($parts['query'])."\r\n";
$out.= "Connection: Close\r\n\r\n";
if (isset($parts['query'])) $out.= $parts['query'];
fwrite($fp, $out);
fclose($fp);
return true;
}
}
//Example of use
backgroundPost('http://example.com/slow.php?file='.urlencode('some file.dat'));
Rachael, thanks for the help.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다