phpsessionmemcachedsessionid

PHP session locking and using Memcache to store sessions


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?


Solution

  • The php-memcached extension supports session locking

    https://www.php.net/memcached

    https://www.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.