I believe globals and superglobals die once the script or session ends. What’s the best way to store an inter-session variable in PHP? In other words, I want every session to have access to a variable. (It will be a boolean for the purpose of synchronizing access to a resource on the server.) Is there a solution besides just storing the value in a file? It would be nice to keep the variable in memory…
EDIT:
I just want to be able to do this:
while ($beingused)
sleep(5);
$beingused = true;
// Do something
SOLUTION:
Thank you all so much for your help. Here’s my solution based on your input.
memcache_pconnect('localhost');
while (memcache_get('inuse') == 'true')
usleep(10);
memcache_set('inuse', 'true');
// Do something
memcache_set('inuse', 'false');
UPDATE
I actually did end up going with a file solution after all. Each time a user interacts with the application, it is fed a script which changes depending on user input. I realized that the existence of the script file could double as a boolean, as was suggested in many of the responses.
I’d use an empty file with certain name (like PID files in Linux). Before executing the command the script checks whether the file exists, and if not, it creates the file. After the shell script is executed, remove the file. But be careful with the file system cache.
Another fast option is to store the flag in the in-memory database like memcache or Redis.