【问题标题】:How can I get this tail function working with SESSIONS?我怎样才能让这个尾函数与 SESSIONS 一起工作?
【发布时间】:2012-01-05 13:38:11
【问题描述】:

我正在用 PHP 编写一个尾部函数,并使用 jQuery 用日志中的任何内容刷新 div 区域。我在 PHP 中使用会话来保存文件指针,因此当再次从 setInterval (jQuery) 调用 PHP 中的函数时,它将知道它在哪里停止并打印文件末尾的任何新内容。出于某种原因,自从我开始使用会话以来,直到任务完成,我才能看到它流式传输到浏览器。请帮我解决一下这个。谢谢!

这是我的 jQuery:

setInterval(function() {
    $.get("ajax.php?function=tail", function(data) {
        $("#tail").append(data);
    }, 'html');
}, 2000);

这是我的 PHP:

function tail() {
    $file = "/path/to/the/log/file.log";
    $handle = fopen($file, "r");

    if(isset($_SESSION['ftell'])) {   
        clearstatcache();
        fseek($handle, $_SESSION['ftell']); 

        while ($buffer = fgets($handle)) { 
            echo $buffer . "<br />";
        }   

        fclose($handle);
        $_SESSION['ftell'] = ftell($handle);        
    } else {
        fseek($handle, -1024, SEEK_END);
        $_SESSION['ftell'] = ftell($handle);
    }
}

【问题讨论】:

    标签: php javascript jquery setinterval tail


    【解决方案1】:

    尝试在http://php.net/manual/en/function.ob-flush.php 回显后刷新缓冲区

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-23
      • 2014-08-19
      • 2013-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多