PHP session locking and using Memcache to store sessions

烈酒焚心 提交于 2019-12-11 02:46:35

问题


I have a standard html page that has a few img tags, each of these are pointing to a php file on our server. When the php file is loaded, it saves some data to the session before then generating an image.

This data in the session from each of the scripts is then used in other script further in our application.

The php file to generate the image starts with session_start and when using the native PHP session handler all seems to work great. Each execution is done correctly and the full session data can be retrieved.

If I enable memcache to save the sessions then problems start to occur. It would appear that each of the PHP scripts are executed at the same time and thus when saving to the session only the last to execute actually stores any data.

After further investigation into Memcache I found that the latest stable version does not support session locking, and only in 3.0.4 do they introduce this - http://pecl.php.net/package/memcache/3.0.4

Is it safe to use 3.0.4 when it is only in beta?

Is there any alternative or a way around this issue in 2.2.7?

Please let me know if you would like to see examples of code.

Thanks.


回答1:


the php-memcached extension supports session locking

http://us3.php.net/memcached

http://us1.php.net/manual/en/memcached.sessions.php

the memcache and memcached extensions look syntactically similar so it may not be too much of a headache to give it a try. (memcached has a stable version 2.1.0 released 2012-08-07).


if you are set on using memcache 2.2.7 you will most likely have to implement the lock yourself by setting some "session_is_locked" variable in your session and then releasing/unsetting it when the script is done writing to the session. Then you'd always need to check if that variable is set before continuing with any scripts which write to the session.



来源:https://stackoverflow.com/questions/21044608/php-session-locking-and-using-memcache-to-store-sessions

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