You are here
Home > Web Hosting > PHP > How to Save PHP Sessions in Memcached

How to Save PHP Sessions in Memcached

Storigng PHP Sessions in memcached

Memcached is a high performance storage engine designed for storing chunks for data, so it is great for storing sessions in it.  The upside of doing this you notice a performance benefit from not writing these disk or a database is that you gain a performance increase. The downside is that memcached isn’t saving anything, so if memcached restarts all of the users will need to login again.


This guide assumes you have a  working installation. If you do not please see How to install Memcached.

Configure PHP for Memcache

Make sure the memcache PHP extension is installed

php -i|grep memcache

If nothing returns go ahead and install it:

pecl install memcache

Find the location of your php.ini

# php -i|grep "Loaded Configuration File"
 Loaded Configuration File => /usr/local/php7/etc/php.ini

Open the php.ini file and add the following line at the top:

extension="memcache.so"

Configure PHP To Store Session Data In Memcached

You will want to edit your php.ini again and find the following lines:

session.save_handler =
session.save_path =

You will then want to update them to the following and uncomment them:

session.save_handler = memcache
session.save_path = 'tcp://127.0.0.1.1:11211'

The reason we selected the PHP extension memcache vs memcached, is that the memcached extension is not available in  PHP 7. Go ahead and save the file.

Restart your webserver

service nginx restart

or

service httpd restart

And your session data should now be stored in memcached

 

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz
Top