我的网站上有一个PHP脚本,它是使用jquery ajax函数调用的。基本上,用户在我的网站上按下一个按钮,它向这个PHP脚本发送一个ajax调用,然后使用proc_open在我的服务器上调用python脚本。当我在 Firefox 中测试它时,这一切都可以正常工作,但是当我在其他浏览器(chrome、safari、移动野生动物园)中测试它时,脚本无限循环。我还尝试直接调用 PHP 脚本(通过将 url 粘贴到浏览器中)以检查它在我的 jquery 中是否是一个无限循环,我得到了相同的结果(在 ff 中运行良好,但在所有其他浏览器中无限循环)。据我了解,PHP代码是在服务器端执行的,然后将输出发送到客户端浏览器,所以我无法弄清楚为什么该脚本仅在Firefox中运行。我还尝试将 SSH 写入我的服务器并从命令行运行 python 脚本,它运行良好,所以我认为这不是 python 脚本的问题。
这是我的 php 代码:
<?php
require('../include/constants.php');
session_start();
if(!isset($_SESSION['logged_in'])||!$_SESSION['logged_in'])
{
header('location:../login.php');
}
$link = mysql_connect(DB_SERVER,DB_USER,DB_PASSWORD);
mysql_select_db(DB_NAME);
$q = "SELECT `cupiduser`,`cupidpass` FROM `users` WHERE `email`='".$_SESSION['email']."' LIMIT 1;";
$result = mysql_query($q);
$row = mysql_fetch_assoc($result);
$minage = $_GET['min'];
$maxage = $_GET['max'];
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("pipe", "w") // stderr is a file to write to
);
$cmd = 'python ~/data/users/demo2.py "'.$row['cupiduser'].'" "'.$row['cupidpass'].'" "'.$minage.'" "'.$maxage.'"';
$proc = proc_open($cmd, $descriptorspec, $pipes);
if(is_resource($proc))
{
fclose($pipes[0]);
$count = fread($pipes[1], 512);
fclose($pipes[1]);
fclose($pipes[2]);
$exit = proc_close($proc);
}
echo trim($count);
ob_end_flush();
ob_flush();
flush();
ob_start();
?>
python代码只是运行一些计算,然后在最后返回一个数字,如下所示:
print str(count)
发送location
标头后,您似乎忘记退出脚本。见下文。
<?php
require('../include/constants.php');
session_start();
if(!isset($_SESSION['logged_in'])||!$_SESSION['logged_in'])
{
header('location:../login.php');
die(); // Exit script
}
注意:这可能是也可能不是循环的根本原因,但这是必须在代码中解决的问题。
为什么需要手动退出脚本?
header()
调用仅向浏览器发送一小串文本。但是,PHP 不知道header('location:...')
和header('foo:...')
之间的区别......它只知道发送到header()
函数的文本需要发送到浏览器。浏览器可能会立即切断与服务器的连接,也可能不会 - 这实际上取决于他们。因此,如果您不希望在header('location:...')
调用后调用任何代码(应该始终如此,因为依靠浏览器来保持连接活动是不可靠的),那么您应该在发送location
标头后立即调用die()
或exit()
(它们是一回事)。
例
<?php
header("location: http://www.example.com/");
die();
我敢打赌问题出在 header() 函数下。尝试在没有此代码行的情况下运行脚本。