如何刷新彗星中的会话? [英] How do i refresh the session in comet?

查看:128
本文介绍了如何刷新彗星中的会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在页面上放置类似彗星的iframe,但是当会话更改iframe中的信息时,不是,并且我在iframe中做了$_SESSION变量的转储,它没有改变.

I want to have a comet-like iframe on my page, but when the session changes the info in the iframe does not and i did a dump of the $_SESSION variable in the iframe and it was not changing.

我的问题是,当客户端$_SESSION更改时,如何更新彗星iframe中的$_SESSION变量?

My question is, how do i update the $_SESSION variable in the comet iframe when the client $_SESSION changes?

非常感谢^ _ ^

使用代码更新:

客户端:

<?php
    session_start();
    if(!isset($_SESSION['count'])) $_SESSION['count'] = 0;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Test Comet</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type='text/javascript' src='../js/jquery.js'></script>
    <script type="text/javascript">
        function test_function(msg){

            $('p').html(msg)

        }

        $('div').click(clickMe);

        function clickMe(event){
            $.ajax({
                url: 'addSess.php'
            })
        }
    </script>
  </head>
  <body>
      <p>This is a test</p>
      <div>click me</div>
    <iframe src="output.php" width="0" height="0" style="display: none;"></iframe>
  </body>
</html>

comet iframe(output.php):

comet iframe (output.php):

<?php
session_start();
ob_start();
echo 'hello';
flush_buffers();
while(true){

    echo "<script>window.parent.test_function('".time().' session: '.$_SESSION['count']."');</script>";

    flush_buffers();

    sleep(1);


}

function flush_buffers(){
    ob_end_flush();
    ob_flush();
    flush();
    ob_start();
}
?>

addSess.php:

addSess.php:

<?php
session_start();

if(!isset($_SESSION['count'])) $_SESSION['count'] = 0;
$_SESSION['count']++;


echo $_SESSION['count'];
?>

我还注意到的另一件事是,它冻结了浏览器进入我的网站的速度,一旦我关闭客户端,其他所有内容就会加载

also another thing i noticed is that its freezing up the browser from going to my site, as soon as i close the client, everything else loads

推荐答案

只要对$_SESSION进行了更改,所有重新加载到执行var_dump($_SESSION);的iframe都应看到新的会话.您是否正确使用 session_start()?

As long as you make changes to the $_SESSION, all reloads to the iframe that does var_dump($_SESSION); should see the new session. Are you properly using session_start()?

请更新一些代码,我们将为您提供帮助.

Update with some code please and we'll see how we can help.

更新

首先,您的彗星页面需要set_time_limit(0);,因此它可以无限期运行(连接仍然可以断开,您需要执行一些操作以重新启动彗星连接)

First off, your comet page needs set_time_limit(0); so it runs indefinitely (the connection can still get cut off and you'd need to do something to restart the comet connection)

第二,输出缓冲可能会给您带来错误,从某种意义上说,PHP有点奇怪,因此我的本地测试不适用于您的代码.如果我添加以下行:

Second off, output buffering might be giving you errors, PHP is wonky in that sense, my local test doesn't work with your code because of it. If I add the line:

<iframe src="output.php" width="0" height="0" style="display: none;" onload="alert('Iframe Loaded');"></iframe>

我在更新主页的同时收到警报,这意味着刷新缓冲区实际上无法刷新缓冲区.当代码在这里工作时,我会返回.

I get the alert at the same time as the update to the main page, meaning flush buffers failed to actually flush buffers. I'll return when I get the code working here.

UPDATE2

叹息.简单...

$('div').click(clickMe);

需要在HTML DOM加载后调用.尝试:

Needs to be called after the HTML DOM loads. try:

$(function(){
    $('div').click(clickMe);
});

此外,您还应该考虑将彗星代码更改为此:

Also you should consider changing your comet code to this:

<?php
session_start();
ob_implicit_flush(true);
ob_end_flush();
echo 'hello';
while(true){
    echo "<script>window.parent.test_function('".time().' session: '.$_SESSION['count']."');</script>";
    sleep(1);
}
?> 

通过执行 ob_implicit_flush ,您不需要不断调用flush_buffers.您还需要ob_end_flush取消输出缓冲.

By doing ob_implicit_flush you don't need to call flush_buffers constantly. You also need ob_end_flush to cancel output buffering.

您还应该注意,需要 session_destroy .否则,您的站点将等待彗星会话关闭,然后才能开始新的会话.

You should also take note that session_destroy is needed. Otherwise your site waits for the comet session to close before it can start a new session.

session_start()实际工作的方式,是等待会话变为可用.这意味着您需要使用 session_id 为彗星生成不同的会话或使用文件...或采用其他技术.

The way session_start() actually works, is it waits for the session to become available. This means you need to use session_id to generate a different session for your comet or use files... or employ some other technique.

这篇关于如何刷新彗星中的会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆