Web Development

Store PHP Session in MemCache

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.

[cc lang=”apache”]
session.save_handler = files
session.save_path = “/session save path/”
[/cc]

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.

[cc lang=”apache”]
session.save_handler = memcache
session.save_path=”tcp://server:port?persistent=1&weight=1&timeout=1″
[/cc]

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.

Shares:
  • Store PHP Session in MemCache | PHP | Syngu
    January 22, 2012 at 12:24 pm

    […] By defualt PHP stores the session in file and these setting are defined in php.ini file.    PHP Read the original post on DZone… […]

    Reply
  • Wenceslaus
    Wenceslaus
    January 22, 2012 at 4:57 pm

    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….

    Reply
  • Wenceslaus
    Wenceslaus
    December 20, 2019 at 2:54 pm

    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….

    Reply
  • Selim Güleryüz
    Selim Güleryüz
    January 23, 2012 at 3:46 am

    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 ?

    Reply
    • Avinash
      January 23, 2012 at 10:40 pm

      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 :)

      Reply
    • Stefan
      Stefan
      April 5, 2012 at 12:22 pm

      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.

      Reply
  • Selim Güleryüz
    Selim Güleryüz
    December 20, 2019 at 2:54 pm

    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 ?

    Reply
  • […] Avinash on Jan 26, 2012 in Caching, PHP | 0 comments Few Days back I have posted about storing the PHP Session in MemCache Server. Before this article I have also written about setting up PHP Session Path. With this article you […]

    Reply
  • kyler
    January 27, 2012 at 3:34 am

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

    Reply
  • kyler
    December 20, 2019 at 2:54 pm

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

    Reply
  • […] Memcached is very useful for increasing the site performance. There are several ways in which you can increase the site performance with MemcacheD, I have already written one of the way on this site which you can find at Save Session in Memcached. […]

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *