如何刷新 Comet 中的会话?
我想在我的页面上有一个类似彗星的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只要您对
$_SESSION
进行更改,所有执行var_dump($_SESSION);
的 iframe 重新加载都应该看到新会话。您是否正确使用 session_start() ?请更新一些代码,我们将看看如何提供帮助。
更新
首先,你的 comet 页面需要
set_time_limit(0);
所以它会无限期地运行(连接仍然可能被切断,你需要做一些事情来重新启动彗星连接)其次,输出缓冲可能会给您带来错误,从这个意义上来说,PHP 很不稳定,因此我的本地测试无法使用您的代码。如果我添加这一行:
我会在更新主页的同时收到警报,这意味着刷新缓冲区未能实际刷新缓冲区。当我让代码在这里工作时我会回来。
更新2
叹息。简单...
需要在 HTML DOM 加载后调用。尝试:
您还应该考虑将您的彗星代码更改为:
通过执行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 doesvar_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:
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...
Needs to be called after the HTML DOM loads. try:
Also you should consider changing your comet code to this:
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.