Skip to content

Instantly share code, notes, and snippets.

@jwestbrook
Last active January 2, 2016 21:39
Show Gist options
  • Save jwestbrook/8365101 to your computer and use it in GitHub Desktop.
Save jwestbrook/8365101 to your computer and use it in GitHub Desktop.
I had to migrate PHP sessions from file based sessions to Memcached based sessions. I couldn't shutdown the servers involved for the split second it would take because all of my users would lose their current sessions. I put this together to migrate the file based sessions to memcached sessions.
<?php
$m = new Memcached();
$m->addServer('localhost', 11211);
$sessions = scandir("/var/lib/php/session/");
if($sessions)
{
foreach($sessions as $s)
{
if(!in_array($s,array('.','..')))
{
$session_name = str_replace("sess_","",$s);
$session_contents = file_get_contents("/var/lib/php/session/".$s);
$m->set("memc.sess.key.".$session_name,$session_contents);
print ".";
}
}
}
?>
@jwestbrook
Copy link
Author

Few things to change for your environment.

  • memcached server
  • memcached port
  • PHP session file location
  • PHP session file prefix
  • PHP session memcache key prefix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment