会话中的Apache崩溃开始for(;;)循环(长轮询)

时间:2015-09-01 19:52:56

标签: php

PHP代码:

<?php

header('Content-Type: application/json; charset=utf-8');
set_time_limit(0);
clearstatcache();
require 'pdo.php';

if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

$user_id = isset($_SESSION['user_id']) ? (int)$_SESSION['user_id'] : null;
$user_id_token = isset($_SESSION['user_id_token']) ? $_SESSION['user_id_token'] : null;

if (empty($user_id) and empty($user_id_token) and !is_numeric($user_id)) {
    $arr = array();
    $my = array(
        "stats" => "Disconnected"
    );
    $arr[] = $my;
    echo json_encode($arr);
    exit();
}

$stmt0 = $pdo->prepare(code);
$stmt0->execute();
$rows0 = $stmt0->fetch(PDO::FETCH_ASSOC);
$arr = array();

if (!isset($_GET['lastId']) or empty($_GET['lastId'])) {
    $my = array(
        code
    );
    $arr[] = $my;
    echo json_encode($arr);
    exit();
}

for (;;) {
    $lastId = isset($_GET['x']) ? (int)$_GET['x'] : null;
    $stmt1 = $pdo->prepare(code);
    $stmt1->bindParam(code);
    $stmt1->execute();
    $stmt = $pdo->prepare(code);
    $stmt->bindParam(code);
    $stmt->execute();
    if ($stmt->rowCount() > 0) {
        while ($rows = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $my = array(
                code
            );
            $arr[] = $my;
        }

        echo json_encode($arr);
        break;
    }
    else {
        continue;
    }
}
?>    

当代码转到(;;){

时发生崩溃

1 个答案:

答案 0 :(得分:0)

我不知道这里到底发生了什么:

$stmt1 = $pdo->prepare(code);

据我所知,这个&#34;代码&#34;在循环的后续步骤中不会改变。这意味着,如果你的$ stmt-&gt; rowCount()第一次返回0,代码会一次又一次地无限尝试。