在Memcache中使用PHPSESSID从会话中检索数据

时间:2015-06-04 07:40:51

标签: php magento session memcached

我使用memcahe / d以magento存储会话,如何检索存储在PHPSESSID的会话中的数据?
基本上我需要这样做:

$sessionFilePath =file_get_contents('path/to/session/sess_'.$_COOKIE['adminhtml']);

但是使用基于memcache / d存储而不是文件的系统 让我们假设:

$session_save_path='tcp://10.0.0.1:11211?persistent=1&weight=2&timeout=10&retry_interval=10'

session.save_path

1 个答案:

答案 0 :(得分:0)

假设$saveMethod只能等于memcachememcachedmemcached应该得到Magento的支持,但以防万一..)(在真实的环境中)它也可以是dbfiles):

$locaXml = Mage::getBaseDir('etc').DS.'local.xml';
$xml = new DOMDocument();
$xml->load($locaXml);
$xpath = new DOMXPath($xml);
$entry = $xpath->query("//session_save");
foreach($entry as $ent){
     $saveMethod = trim($ent->nodeValue);
}
$saveMethod=(!empty($saveMethod))? $saveMethod:'files';

$entry = $xpath->query("//session_save_path");
foreach($entry as $ent){
    $session_path = $ent->nodeValue;
}
if(strpos($session_path,'?')!==false){
    $session_path=(explode('?',$session_path));
    $session_path=$session_path[0];
}

$session_path=explode(':',$session_path);
$port= $session_path[count($session_path)-1];
unset($session_path[count($session_path)-1]);
$host=implode(':',$session_path);
if($saveMethod=='memcache'){
    $m = new Memcache();
    $m = memcache_connect($host, $port);
}
else if($saveMethod=='memcached'){
    $m = new Memcached();
    $m->addServer($host, $port);
}

$session_data= $m->get({{PHPSESSID_VALUE}});