我有两个PHP脚本 - 我们称之为脚本A和脚本B.当用户进行某种交互时,脚本A开始运行。脚本A需要在后台运行脚本B,因此在脚本B仍在运行时返回脚本A的结果。我可以在脚本A中执行此操作:
exec('scriptB.php &')
但是,因为我不允许共享托管exec
。此外,Ajax解决方案(在客户端启动两个脚本)将无法工作,因为脚本B必须运行 - 我不能让用户恶意停止脚本运行。
是否有任何解决方案不涉及使用shell命令或Ajax?
提前致谢!
答案 0 :(得分:0)
我最终使用这种方法: 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,谢谢你的帮助。