Don Rhummy
Don Rhummy

Reputation: 25860

How can my code know if the client/browser has received the session id yet?

When creating a session and storing data into it, if the session id has not yet been returned to the client, then multiple calls from the client to the server will result in different data showing in the $_SESSION object.

For example:

  1. Client uses ajax to request a page: "/very_slow.php"
  2. "very_slow.php" sets a session variable: $_SESSION[ "status" ] = "started"
  3. "very_slow.php" then starts doing something that takes 30-40 seconds
  4. Client uses ajax to immediately call another page: "/check_session_variable.php" that checks $_SESSION[ "status" ]

If step #2 had to create the session (session_start();) but has not yet returned to the client, then step #4 will not see the new value in $_SESSION[ "status" ].

So I need to be able to check whether the client has a session id yet. How do I do this?

Checking session_id() doesn't work, because if any code has called session_start(), then I'll get a value returned even if the client hasn't yet received it in a response.

Upvotes: 0

Views: 99

Answers (1)

Alex Blex
Alex Blex

Reputation: 37048

Before Client uses ajax to request a page: "/very_slow.php" the client probably opened a page with some HTML and Javascript, which makes the ajax request. Start the session there. Session ID will be sent to the client as HTTP header and will be used by xhr on step 1. It will guarantee session cookie for steps 1..4.

Upvotes: 2

Related Questions