如何检查管理员用户是否已登录前端,因为前端和后端的cookie被拆分了?
答案 0 :(得分:1)
修改强>
显然,如果session_save_path
是"设置"在memcache上(它将会话存储到memcache中,忽略session_save
节点,会话将保存到memcache中
我在网上搜索了这个问题(stackoverflow和magento堆栈已经打开了一些这样的问题),但结果不佳,除了一个谈论检索会话文件的德国人,所以我决定将这个解决方案扩展到所有的session_save
中app/etc/local.xml
节点的可能值,我已将此代码放在Helper/Data.php
类中。
它远没有好,但作为一个起点可能没问题
// Redis library: https://github.com/colinmollenhour/Cm_RedisSession
public function isAdmin(){
if(array_key_exists('adminhtml', $_COOKIE)){
$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)) ? trim($saveMethod):'files';
if(Mage::getConfig()->getModuleConfig('Cm_RedisSession')->is('active', 'true') && $saveMethod=='db'){
$entry = $xpath->query("//redis_session");
if($entry->length>0)
$saveMethod='redis';
}
switch ($saveMethod) {
case 'db':
$read = Mage::getModel('core/resource')->getConnection('core_read');
$query = $read->select()->from(Mage::getSingleton('core/resource')->getTableName('core/session'))
->where('session_id=?',$_COOKIE['adminhtml'])
->limit(1);
$sessionDb = $read->fetchAll($query);
if(count($sessionDb)==0)
return false;
$session_data = $sessionDb[0]['session_data'];
break;
case 'files':
$session_path=Mage::getBaseDir('session').DS.'sess_'.$_COOKIE['adminhtml'];
if(!is_file($session_path))
return false;
$session_data = file_get_contents($session_path);
break;
case 'memcached':
case 'memcache':
if(!isset($session_path)){
$entry = $xpath->query("//session_save_path");
foreach($entry as $ent){
$session_path = $ent->nodeValue;
}
}
$timeout=null;
if(strpos($session_path,'?')){
$session_path=(explode('?',$session_path,2));
$host_port=$session_path[0];
preg_match('@\&?timeout=([0-9]+)\&?|$@',$session_path[1],$match);
$timeout= (isset($match[1]))? $match[1]:null;
}
$host_port=explode(':',$host_port);
$index=count($host_port)-1;
$port= $host_port[$index];
unset($host_port[$index]);
$host=implode(':',$host_port);
if($saveMethod=='memcache'){
$m = new Memcache();
$m = memcache_connect($host, $port,$timeout);
if(!$m){
echo "Can't connect to Memcache server";
return false;
}
}
else if($saveMethod=='memcached'){
$m = new Memcached();
$m->addServer($host, $port);
}
$session_data= $m->get($_COOKIE['adminhtml']);
break;
case 'redis'://Tested by Elias Soares
$session_data = Mage::getResourceSingleton('core/session')->read($_COOKIE['adminhtml']);
if(!is_string($session_data))
$session_data=serialize($session_data);
break;
}
if(isset($session_data) && strpos($session_data,'Mage_Admin_Model_User'))
return true;
return false;
}
return false;
}
答案 1 :(得分:1)
另请考虑Cross Area Sessions。它会按照你的要求做更多,但这是一个相对年轻的项目。我认为这是一个值得关注的问题。