Magento如何检查管理员是否登录(前端)


Magento how to check if Admin is logged in (Frontend)

如何检查admin用户是否在前端登录,因为前端和后端cookie是分开的?

还要考虑跨区域会话。它可以满足您的要求,甚至更多,但它是一个相对年轻的项目。我认为这是值得关注的。

EDIT
显然,如果session_save_path在memcache上"设置"(它将会话存储到memcache中,session_save节点被忽略,会话将被保存到memcache中

我在网上搜索了这个问题(stackoverflow和magento堆栈已经打开了一些这些问题),但是除了一个正在谈论检索会话文件的德国人之外,结果很差,所以我决定将这个解决方案扩展到app/etc/local.xmlsession_save节点的所有可能值,我已经将此代码放在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['a‌​dminhtml']);
                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;
    }

原始来源:Magento: pr fe Admin Session/Login im Frontend