如何使用Ajax显示php循环的步骤?(长轮询…)
我有一个PHP服务器端脚本,它在foreach循环中,通过SSH连接到路由器并执行一些配置块如何使用Ajax显示php循环的步骤?(长轮询…),php,jquery,ajax,long-polling,Php,Jquery,Ajax,Long Polling,我有一个PHP服务器端脚本,它在foreach循环中,通过SSH连接到路由器并执行一些配置块 <?php function apply($hostname){ $ssh = new Net_SSH2($hostname); if (!$ssh->login($this->username, $this->password)) { exit('Login Failed'); } $i = 1; foreach($
<?php
function apply($hostname){
$ssh = new Net_SSH2($hostname);
if (!$ssh->login($this->username, $this->password)) {
exit('Login Failed');
}
$i = 1;
foreach($NMConf->configuration as $conf_step){
$ssh->write($conf_step); //Can take 5s...
$this->ssh_errors[] = $ssh->read("#");
echo "Step".$i." ok.. <br/>";
$i++;
flush();
sleep(1);}
} ?>
我想在输出div中显示不同的步骤,如
Step 1 (working....)
然后
然后
Step 1 OK!
Step 2 OK!
我该怎么做
我看了一下长轮询的东西&反向ajax,但它似乎有点棘手
任何想法
谢谢我的建议,会话中的内容信息,并尝试询问php脚本的状态 创建将读取会话的脚本
<?php
$step = $_GET["step"];
if($_SESSION["last_step"] != $step) //just to avoid same info twice
{
if(isset($_SESSION['step'.$step])
echo $_SESSION['step'.$step];
else
}
else { echo '10' }; //this will tell you that next step is not executed jet
?>
完成所有步骤后,只需清除间隔clearInterval(\u int),但您缺少了最重要的一点:PHP只在脚本末尾或您告诉它手动执行时保留$\u会话内容。您只能使用
会话\u write\u close()
-我以后从未尝试再次打开同一个会话,这可能是一个有趣的实验。会话在激活之前是有效的,并且在请求之间使用同一个会话,所以基本上您可以在第一次请求时将值放入会话,其他每一个请求都将能够获得数据,这是您唯一需要的,以确保您的会话针对每个请求启动。我一个字都听不懂您在说什么。要点是:当脚本正在进行中并且您从一个脚本/请求中不断更新$\u会话时,这些内容在其他请求/会话中将不可用,直到您在请求结束时持久化会话数据为止。
Step 1 OK!
Step 2 OK!
<?php
$step = $_GET["step"];
if($_SESSION["last_step"] != $step) //just to avoid same info twice
{
if(isset($_SESSION['step'.$step])
echo $_SESSION['step'.$step];
else
}
else { echo '10' }; //this will tell you that next step is not executed jet
?>
var _int = setInterval(your_method, 1000);