PHP AJAX回调json数据然后再睡觉

时间:2014-09-22 13:51:55

标签: php ajax json

我在这里尝试了几个答案,但都没有效果。

我有这个基本的蜜罐脚本:

  if (!empty($_POST['starttime'])) {

        $current_time = time();
        $json = array();
        $json = reGenerateFormFields();//hold the hashed keys arays for change in the key output.

        if (($current_time - htmlentities(@$_POST['starttime'])) < 4) { // 3 is number of seconds differential
                        $return = array('hp' => true,'message' =>'אנא המתן 3 שניות בין שליחה','key' => $json['2'], 'nonce' => $json['1'],'time' => time());
              echo json_encode($return);
            sleep(7);
            die();

        }
}

我试图回应json消息然后睡觉,但是现在它第一次睡觉然后回复消息,任何想法如何做到这一点好方法?

先谢谢。

1 个答案:

答案 0 :(得分:2)

很可能回声数据只是被缓冲而不是发送,直到睡眠结束并且请求完成。尝试使用flush()强制将输出推送到客户端:

echo json_encode($return);
flush();
sleep(7);
die();

如果失败,您可能会发现填充输出有助于满足服务器/浏览器的最小长度要求以刷新/显示数据:

echo str_pad(json_encode($return),8192," ");
flush();
sleep(7);
die();
相关问题