tags:

views:

58

answers:

1

I have an ajax long-polling request that works fine on the first page load. The problem is when the page is reloaded or a new page is browsed to then the PHP session is locked until the old PHP session times out.

I have the following long polling code...

 while (time() - $time < 55) {

  if ($userid != 0) {

   updateUserSession();
   fetchMessages();
   getTyping();
   //getNotifications();

   if (!empty($response)) {

    header("Content-Type: text/plain");
    echo json_encode($response);
    exit;
   }

  }

  sleep(2);
 }

I have a session_start at the top.

Now, the problem is that the getTyping function might need to set a session, so a session_write_close cannot be called until the long-polling request is done, so I cannot just get the session data and then close it right away.

Can anyone think of a way around this?

Thank you.

A: 

Could you call both session_start() and session_write_close() every time you need access to the session? I guess you might end up with duplicate headers being sent, but that may not be an issue.

edit this won't work because you cannot call session_start() once you have sent anything to the browser

Tom Haigh
I didn't even try that, I thought that you could only start a session once?
Chris