Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2008
    Location
    Belgrade
    Posts
    19
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Ajax+Sessions doesn't allow page close/refresh (Comet)

    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.
    Any ideas?

  • #2
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts
    How is your request being killed when you close the page? By nature, when you call a PHP script, it will run until it errors out, times out, or finishes - closing the web browser will not stop your PHP script from running on the server...it only kills the javascript.

    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.

  • #3
    New Coder
    Join Date
    Jun 2008
    Location
    Belgrade
    Posts
    19
    Thanks
    17
    Thanked 0 Times in 0 Posts
    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.

    PHP Code:
    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); 
    header('Pragma: no-cache');
     

    session_start(); 
    $vreme $_SESSION['vreme'];
    session_write_close();

    $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_start();
            
    $_SESSION['vreme'] = $row->vreme;
            
    session_write_close();
            break;
        }
        
    usleep(25000);
    }

    $mysqli->close();

    echo 
    str_replace("<br />""\n"$row->poruka) . "[BRK]" $row->vreme
    Last edited by okram666; 09-15-2009 at 10:31 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •