How do i refresh the session in comet?
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开发者_StackOverflow社区($_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
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.
精彩评论