/memcache, PHP, session

PHP session handling with memcache

In last two posts I described how to install memcached daemon and memcache extension for PHP. Today I'll show you how to configure your PHP to use memcache to handle sessions.

One thing I should mention is that when installing memcache you should answer yes when asked

1. Enable memcache session handler support? : yes

First of all, lets start two memcached processes just in case one crashes, we’ll have a second one ready right away.

memcached -u root -d -m 512 -l -p 11211
memcached -u root -d -m 512 -l -p 11212

Worth mentioning is that when one memcached process crashes session information will not be transferred to another. New session will be started.

Next edit your php.ini file. Comment out the line with your current session handler setting

; session.save_handler = files

and add two new lines

session.save_handler = memcache
session.save_path = "tcp://localhost:11211, tcp://localhost:11212"

If for any reason you don’t want to edit php.ini file you can set those options directly in your PHP script.

ini_set('session.save_handler', 'memcache');
ini_set('session.save_path', 'tcp://localhost:11211, tcp://localhost:11212');

Restart your web server

service httpd restart

For full list of runtime configuration options go here.