Store PHP Session in MemCache

  • Avinash
  • 8
  • Jan 20, 2012
  • Web Development

By defualt PHP stores the session in file and these setting are defined in php.ini file. It would have two settings for saving the sessions in file.

If you are not sure what is the session save path then you should read this post about Session Save Path in PHP.

Be default it will have below lines in your php.ini file.

session.save_handler = files session.save_path = "/session save path/"
  session.save_handler = files
  session.save_path = "/session save path/"

To save your sessions using memcache you need to change your php.ini lines to below lines. To save your session using memcache you will need below details to get it done.

  1. Memcache Server
  2. Memcache Port

After getting these details you will need to comment above lines and need to place below lines.

session.save_handler = memcache session.save_path="tcp://server:port?persistent=1&weight=1&timeout=1"
  session.save_handler = memcache
  session.save_path="tcp://server:port?persistent=1&weight=1&timeout=1"

After making these changes you will need to restart your web server. Here what we are instructing php to set session handler to memcache and in next line we are defining the save path for session.

You might be surprized Why would you save your PHP sessions in memcache? If you are curious to know what would be the reason then subscribe to our RSS Feed, You will be notified by the email for this answer.

Related Posts

Written by Avinash

Avinash Zala is leading various projects which deals with the various technology involved with the web. A combination of perfect technical and management skills. Avinash would like to chat with you and convert your imagination into the working system. You can get in touch with him on Facebook and Twitter.

View all posts by:

  • Pingback: Store PHP Session in MemCache | PHP | Syngu

  • Wenceslaus

    Hi I have done this just to test but now phpmyadmin does not work is there a way to make them work i am a newbie….

  • Selim Güleryüz

    Thank for post and OK. We can use memcache for storing sessions. Is memcache right for this chose ? Because, memcache just a cache system. Our session datas will be secured ?

    • http://www.xpertdeveloper.com Avinash

      Hi Selim,

      Its a best option if you have multi application server. That is what I am suppose to write on upcoming days. Stay tunes for updates :)

    • Stefan

      Memcached’s problem specifically is that it is cache. It can drop old data (your session), when new data come in.

      I have recommended people to try scache and get rid of using $_SESSION altogether. Scache has built in sessionhandler, but it encourages hard to use its own API instead and split session data to small fragments that are accessed separately.

      It is also suitable for multi front end constructions and has many other fancy features.

  • Pingback: Why We Use Memcache for PHP Sessions? | Expert PHP Developer

  • http://www.ispeech.org/speech.recognition kyler

    thanks for your tips and it’s a nice blog!!!

  • Pingback: Install Memcached on Ubuntu for PHP | Expert PHP Developer