3

I have ocassionally detected a strange problem with PHP sessions.
When I am running two PHP scripts using SAME session ID, second script is stuck until first one is completed.
I guess it is because trying to open same session storage file twice. But possible I am not right.
You will never catch this effect in normal site work, because user usually didn't open two or more pages simultaneously.
However, if you try to get content of a page of the same site using file_get_contents(), you will catch this issue.
Additionally, I have copying my cookies through context, so file_get_contents() trying to re-open same session as already opened in calling script.
As result, I have stucked long-running script (about 5-10 mins) which also disables me to open any new page of same site using same sessionid / login.
How I can to resolve this issue? Did you ever see any beautiful solution for it?

1
  • 1
    You figured it out, a user can't use the same session more than once at the same time, so requests are throttled. A common problem when doing ajax calls to the same file multiple times simultaneously. Commented Apr 13, 2013 at 0:03

2 Answers 2

4

Yes, this is called "session locking" and is normal in PHP.

One solution is not not use sessions, just set cookies for your required persistent information.

Another solution is to implement your own session handler:

http://php.net/manual/en/session.customhandler.php

A detailed walkthrough about custom MySQL session handlers is here:

http://phpmaster.com/writing-custom-session-handlers/

Sign up to request clarification or add additional context in comments.

Comments

3

I also found quite simple solution for this problem. We can use session_write_close(); to unlock session file in script 1, then we can make any file_get_contents(), curl_exec() etc without any worries and after these operations turn session back by session_start(). Checked by myself, works as charm!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.