09-15-2009, 03:58 PM
Ok, I'm working on ajax chat app, but not with regular ajax request per second. It is based on ajax long polling.
So It is pretty simple.
Ajax requests php page -> php sleeps in loop waiting for new data from DB -> when new data show up php echo that data and ajax request is killed.
Even when I close/refresh page the ajax request is killed, so everything is perfect.
But when I use sessions i got problem.
If session_start() is called, than i can't refresh page, or when i close it ajax request is still working in background.
09-15-2009, 04:10 PM
Using sessions shouldn't affect how that works. If you are interested in stopping your PHP when you close the browser, then just have it echo back some result every X seconds and finish executing. If that result means that nothing was found yet, then call the script again, otherwise finish executing.
09-15-2009, 09:43 PM
I found what was wrong... I didn't know that PHP doesn't write sessions until the whole code is executed... So simple session_write_close(); fixed everything.
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', FALSE);
$vreme = $_SESSION['vreme'];
$time = time();
$mysqli = new mysqli('localhost','root','ajaje3budja','karte');
while((time() - $time) < 29)
$result = $mysqli->query("SELECT * FROM test");
$row = $result->fetch_object();
if($row->vreme !== $vreme)
$_SESSION['vreme'] = $row->vreme;
echo str_replace("<br />", "\n", $row->poruka) . "[BRK]" . $row->vreme;