Created
November 29, 2017 16:08
-
-
Save kbsali/bc5673a7f212afbde73e2aac20166420 to your computer and use it in GitHub Desktop.
Migrate php session from file to redis|memcache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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