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
";
$out.= "Host: ".$parts['host']."
";
$out.= "Content-Type: application/x-www-form-urlencoded
";
$out.= "Content-Length: ".strlen($parts['query'])."
";
$out.= "Connection: Close
";
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.