Using same session ID within two PHP scripts at same time

天涯浪子 提交于 2019-12-01 06:06:40

问题


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:


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/




回答2:


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!



来源:https://stackoverflow.com/questions/15982654/using-same-session-id-within-two-php-scripts-at-same-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!