如何刷新 Comet 中的会话?

发布于 2024-10-30 19:58:30 字数 1863 浏览 2 评论 0原文

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

我的问题是,当客户端 $_SESSION 发生变化时,如何更新 comet iframe 中的 $_SESSION 变量?

非常感谢 ^_^

使用代码更新:

客户端:

<?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):

<?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:

<?php
session_start();

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


echo $_SESSION['count'];
?>

我注意到的另一件事是它冻结了浏览器,无法访问我的网站,因为一旦我关闭客户端,其他所有内容都会加载

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.

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

Thanks so much ^_^

update with code:

client:

<?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):

<?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:

<?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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诗笺 2024-11-06 19:58:30

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

请更新一些代码,我们将看看如何提供帮助。

更新

首先,你的 comet 页面需要 set_time_limit(0); 所以它会无限期地运行(连接仍然可能被切断,你需要做一些事情来重新启动彗星连接)

其次,输出缓冲可能会给您带来错误,从这个意义上来说,PHP 很不稳定,因此我的本地测试无法使用您的代码。如果我添加这一行:

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

我会在更新主页的同时收到警报,这意味着刷新缓冲区未能实际刷新缓冲区。当我让代码在这里工作时我会回来。

更新2

叹息。简单...

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

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

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

您还应该考虑将您的彗星代码更改为:

<?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 来取消输出缓冲。

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

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

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.

UPDATE

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)

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

Sigh. Simplicity...

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

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);
}
?> 

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

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文