Skip to content

Instantly share code, notes, and snippets.

@kbsali
Created November 29, 2017 16:08
Show Gist options
  • Save kbsali/bc5673a7f212afbde73e2aac20166420 to your computer and use it in GitHub Desktop.
Save kbsali/bc5673a7f212afbde73e2aac20166420 to your computer and use it in GitHub Desktop.
Migrate php session from file to redis|memcache
#!/usr/bin/env php
<?php
$host = '127.0.0.1';
$port = null;
$oldPrefix = 'sess_';
$newPrefix = 'sess_';
$sessionDir = '/var/lib/php/session/';
$m = new \Redis();
$m->connect($host, $port);
// $m = new \Memcached();
// $m->addServer($host, $port);
$sessions = scandir($sessionDir);
if (!$sessions) {
die('nothing to migrate');
}
foreach ($sessions as $s) {
if (in_array($s, ['.', '..'])) {
continue;
}
$sessionName = str_replace($oldPrefix, '', $s);
$sessionContents = file_get_contents($sessionDir.$s);
if (!$m->set($newPrefix.$sessionName, $sessionContents)) {
die(sprintf('Could not migrate session %s'.PHP_EOL, $newPrefix.$sessionName));
}
echo '.';
}
die(PHP_EOL);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment