Epsiloncool
Epsiloncool

Reputation: 1473

Using same session ID within two PHP scripts at same time

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?

Upvotes: 3

Views: 2161

Answers (2)

Epsiloncool
Epsiloncool

Reputation: 1473

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!

Upvotes: 3

Petah
Petah

Reputation: 46050

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/

Upvotes: 4

Related Questions