Store PHP Session variables in database rather than filesystem
Why do you ask?
Recently we moved our website from a single server system to a multi server system and we need a fast an efficient way to handle sessions across multiple application servers, and a way to handle it when we add more application servers in the future. The fix was to re-write PHP's core session handler to use a table in our database rather than the file system... basically re-writing the sessionsavepath to point to the database instead of a file directory.
I ended up with a new class that contained all of the modified functions that the session handler typically has (read, open, close, destroy, etc.) Then in whatever files you call session_start() you include your new class and then call it before you start the session... from that point on, all session specific functions are wrapped up in the database and not in the local file system, allowing your app servers to access session info across the board!
VOILA! Load balanced session handling with minimal effort!
Written by Dave Chenault
Related protips
1 Response
I think the most efficient way to handle sessions in php is using Redis server. Redis can be used for cache and for session handling as well. To configure your app to use redis for sessions, you have to edit your index.php file and add script in it. Apart from redis, memcached can also be used, but now redis is a more preferable choice.
Source: Redis as a PHP session handler.