我有两个PHP脚本-我们将它们分别称为脚本A和脚本B。当用户进行某些交互时,脚本A开始运行。脚本A需要在后台运行脚本B,因此在脚本B仍在运行时返回脚本A的结果。我可以在脚本A:中执行此操作exec('scriptB.php &')
,但是由于我位于共享主机上,因此exec
是不允许的。另外,由于必须运行脚本B,因此Ajax解决方案(在客户端上启动两个脚本)将无法工作-我不能让用户恶意停止脚本的运行。
是否有不涉及使用shell命令或Ajax的解决方案?
提前致谢!
我最终使用了这种方法: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),谢谢您的帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句