1

I have a front end script that calls a PHP page via ajax. That PHP page can run for 20-30 seconds. During its run, it sets a $_SESSION variable multiple times to indicate status/progress.

While it's running, I want to have an interval in JavaScript that calls another script via ajax that simply checks that $_SESSION variable and returns its current value.

My question: will the second script see the updated value in the $_SESSION even while the first script continues to run?

First Script

//We check session started...

//Start
$_SESSION[ "status" ] = "started";

//Some long function
doSomethingSlow();

//Progress
$_SESSION[ "status" ] = "did something";

//Some long function
doSomethingElseSlow();

//Done
$_SESSION[ "status" ] = "started";

Second Script

//We check session started...

//Return the status
return json_encode( array( "status" => $_SESSION[ "status" ] ) );
Don Rhummy
  • 24,730
  • 42
  • 175
  • 330

1 Answers1

1

I think it will depend on whether the first script had to create the session, or it was already created before the first script was run. To continue a session, the client has to have received a response from a script that sends the session ID cookie. So the first requirement is that the session be started by some script that completes before you send the first AJAX request. This could be the script that sends the page that contains the AJAX calls.

Another issue is that PHP normally locks the session file when the session is opened, and updates it when a script terminates. So if a second script tries to access the same session while the first script is running, it will hang waiting for the lock. To force it to update immediately and unlock the file after assigning a session variable, you need to call session_write_close(). And after closing the session file, you need to reopen it again with session_start() before writing to the session variable.

//Start
$_SESSION[ "status" ] = "started";
session_write_close();

//Some long function
doSomethingSlow();

//Progress
session_start();
$_SESSION[ "status" ] = "did something";
session_write_close();

//Some long function
doSomethingElseSlow();

//Done
session_start();
$_SESSION[ "status" ] = "started";
session_write_close();
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Is there a way to know if the client has received the session key yet? My code is in a CMS so it's possible some other code called `session_start()` but the session was never sent to the client in the cookie. Is there a way to know that? – Don Rhummy Feb 04 '16 at 18:53
  • You can call `session_id()` before calling `session_start()`. If there's no session yet, it will return an empty string. – Barmar Feb 04 '16 at 20:10
  • Thanks. If some code already called `session_start()` though, wouldn't that make `session_id()` return a value? And it would return a value even though it was never sent to the client. – Don Rhummy Feb 04 '16 at 21:07
  • Yes. That's why you have to check it first. I saw [another question](http://stackoverflow.com/questions/6249707/check-if-php-session-has-already-started) where he used `isset($_COOKIE['PHPSESSID'])` before calling `session_start()`. But he was having problems with that, and the recommended solution was to use `session_id()` or `session_status()` to see if there's already a session in progress. So there doesn't seem to be a reliable way to distinguish a newly-started session from a continued one. – Barmar Feb 04 '16 at 21:15
  • So on the PHP side, there's no way to know. Is there a way on the client side in JavaScript to check for a session id? – Don Rhummy Feb 04 '16 at 21:19
  • You can check if there's a `PHPSESSID` cookie. – Barmar Feb 04 '16 at 21:20