Code to store PHP sessions within memcached.

Reinis Rozitis roze at
Fri Nov 17 08:06:23 UTC 2006

Hmm why dont you just use the nice feature of php to have your own session 
handling functions override the default which could be transparent to the 
rest of the code?
We use something like this:

function mc_client() {
 $smc =& $GLOBALS['smc'];
 if (!is_object($smc)) {
  $smc = new Memcache;
   // and the rest of the servers you need with memcache_add_server()
 return $smc;

function mc_ses_open($path, $name) { return true; }
function mc_ses_close() { return true; }

function mc_ses_read($id) {
 $client = mc_client();
 return $client->get($id);

function mc_ses_write($id,$data) {
 $client = mc_client();

function mc_ses_destroy($id) {
 $client = mc_client();
 return true;
function mc_ses_gc($maxlt) { return true; }

And then simply session_start() and $_SESSION['blabla'] = 'value' and thats 
all.. (you dont need to stick with cookies cause that is handled by default 
from php side)

Tested on 30k concurent users/sessions online with 7 switching (roundrobin) 
www nodes and it works just fine..
The conclusion we got its best to keep some seperate Memcached instance just 
for the sessions that way you dont mix it with other type of data caches and 
because of LRU push the needed items out.


----- Original Message ----- 
From: Kevin Burton
To: Memcached list
Sent: Friday, November 17, 2006 7:30 AM
Subject: Code to store PHP sessions within memcached.

Someone wanted the PHP code were were using to store our sessions within 

Well here you go.

replace tailrank_ to something less proprietary.... and you'll need your own 
version of tailrank_memcache_get_instance to get a copy of your memcached 

you to call session_start() at the beginning of your script and 
session_commit() at the end.

you'll also need to change the domain from to your domain 

If you make any changes or fix any bugs please let me know.

This is hereby licensed under the GPL, BSD, and public domain so have fun! 


 * Send mail but also include the From header.
function tailrank_session_start() {

    //For machines that already have this session loaded this will merely be

    global $_SESSION;
    $my_memcache = tailrank_memcache_get_instance();

    $cookie = $_COOKIE[ "SESSIONID" ];

    if ( $cookie != null ) {

        $key = "session3:" . $cookie;
        $session_data = $my_memcache->get( $key );

        if ( $session_data != null ) {
            $_SESSION = unserialize( $session_data );

    } else {

        //call the default php session start code to init the session on 

        $cookie = rand( 0, 100000000000 );

        $_COOKIE[ "SESSIONID" ] = $cookie;

        //create a new local aray for the session.

        $expire = time() + 365 * 24 * 60 * 60 * 24;
        setcookie( 'SESSIONID', $cookie, $expire, "/", "" );



function tailrank_session_commit() {

    //serialze $_SESSION

    global $_SESSION;
    $my_memcache = tailrank_memcache_get_instance();

    if ( $_SESSION != null ) {

        $session_data = serialize( $_SESSION );
        $cookie = $_COOKIE[ "SESSIONID" ];
        $key = "session3:" . $cookie;

        $my_memcache->set( $key, $session_data, false, 600 );




Location: San Francisco, CA
AIM/YIM: sfburtonator
Skype: burtonator
Cell: 415-637-8078 

More information about the memcached mailing list