2011-02-09 76 views
7

我有一臺安裝了Magento 1.4.0.1的Web服務器。我有另一個網站與它共享憑據。我已經設法檢查客戶是否已登錄(在更改Magento中的cookie位置之後),但是當我試圖確定管理員是否已登錄時,情況變得複雜。我只能得到正確的答案對於我要求的第一個會話(客戶或管理員,第二個從未登錄)。Magento - 檢查管理員和客戶是否登錄

我怎樣才能得到兩個答案?

這裏是我使用來測試出代碼:


require_once '../app/Mage.php'; 
umask(0) ; 

Mage::app(); 

// Checking for customer session 
Mage::getSingleton('core/session', array('name'=>'frontend')); 
$session=Mage::getSingleton('customer/session', array('name'=>'frontend')); 

if ($session->isLoggedIn()) { 
    echo "Customer is logged in"; 
} else { 
    echo "Customer is not logged in"; 
} 

// Checking for admin session 
Mage::getSingleton('core/session', array('name'=>'adminhtml')); 
$adminsession = Mage::getSingleton('admin/session', array('name'=>'adminhtml')); 

if($adminsession->isLoggedIn()) { 
    echo "Admin Logged in"; 
} else { 
    echo "Admin NOT logged in"; 
} 
用這樣的代碼

因此,管理員從未登錄如果你把對管理的第一部分,那麼。客戶從未登錄過。似乎我錯過了兩個請求之間的界限。

這可能比這個懸而未決的問題同樣的問題:Magento how to check if admin is logged in within a module controller

這似乎是一個流行的問題,但我無法找到合適的解決辦法...

感謝您的幫助!

+0

這是最接近答案的帖子是這一個http://www.magentocommerce.com/boards/viewthread/50307/#t274955,但我不能讓它工作。在同一個線程中的另一條消息談到將兩個驗證分離到不同的PHP文件,但創建類與我調用的功能產生相同的結果... – Melanie

回答

2

你需要做的是切換會話數據。你可以用下面的代碼做到這一點:

$switchSessionName = 'adminhtml'; 
$currentSessionId = Mage::getSingleton('core/session')->getSessionId(); 
$currentSessionName = Mage::getSingleton('core/session')->getSessionName(); 
if ($currentSessionId && $currentSessionName && isset($_COOKIE[$currentSessionName])) { 
    $switchSessionId = $_COOKIE[$switchSessionName]; 
    $this->_switchSession($switchSessionName, $switchSessionId); 
    $whateverData = Mage::getModel('mymodule/session')->getWhateverData(); 
    $this->_switchSession($currentSessionName, $currentSessionId); 
} 

protected function _switchSession($namespace, $id = null) { 
    session_write_close(); 
    $GLOBALS['_SESSION'] = null; 
    $session = Mage::getSingleton('core/session'); 
    if ($id) { 
     $session->setSessionId($id); 
    } 
    $session->start($namespace); 
} 
0

這裏是我用什麼..

Mage::getSingleton('core/session', array('name'=>'adminhtml')); 
$session = Mage::getSingleton('admin/session');; 
if (!$session->getUser()) 
{ 
    die("You aren't an admin!"); 
} 
+0

謝謝,但在我的情況下,兩個用戶是不一樣的(我們有分開的管理員和客戶帳戶)。所以我仍然有同樣的問題:在客戶會話之後檢查管理會話時,getUser()中沒有任何內容。 – Melanie

4

我發現「bug的特徵」,從另一個角度(嘗試登錄從adminside客戶),但還是找到了癥結。

問題在於session_name()函數。如果你去了Mage_Core_Model_Session_Abstract_Varien,你會看到會話對象使用了標準的PHP會話函數,PHP不能同時處理兩個會話。

您管理員的會話ID存儲在cookie adminhtml中,而對於客戶端,您的會話ID位於前端cookie中。然後在adminside中,您有由adminhtml cookie初始化的會話ID。在管理方面,您的客戶/會話對象存儲在PHP會話中的$ _SESSION ['customer'](未檢查確切鍵)之類的內容中,以存儲在adminhtml cookie中。這意味着客戶/會話對象在magento的管理員和客戶端部分內引用不同的會話。

+0

嗨Zebooka!謝謝你的解釋!我現在已經讀過好幾遍了,但是我的大腦並沒有解決它!我得到該客戶和管理員使用不同的cookie。我知道那時PHP只能識別一個會話。現在我不確定的部分是關於是否可以挖掘$ _SESSION從管理員那裏查找客戶對象以及如何執行此操作。我已經打印出$ _SESSION,這是一個巨大的陣列..沒有'客戶'鍵。我比較了客戶登錄時的內容,但沒有發現差異...... – Melanie

+1

您在PHP中只有一個會話。 Magento爲管理員和客戶端使用不同的cookie(adminhtml和前端)。模型admin/session和customer/session被保存在$ _SESSION裏面,有一些數組鍵(比如admin和customer)。但是,當你在管理端和客戶端時,$ _SESSION數組是不同的 - 因爲你在不同的會話cookie(adminhtml和frontend)中有不同的會話ID。訪問前端會話的唯一方法是獲取前端cookie內容,並以某種方式手動加載var/session/sess_SESSIONID內容的客戶/會話模型。 – Zebooka

-2

下面是一個簡單的腳本來檢查管理員登錄與否,如果Magento.You的記錄獲得管理細節可以打電話到會話和呼叫用戶的功能獲取所有細節。

$userDetails = Mage::getSingleton('admin/session'); // Get data from the session 
$userID  = $userDetails->getUser()->getUserId(); // Get user ID 
$userID  = $userDetails->getUser()->getEmail(); // Get user Email 

詳情請參閱http://webexplorar.com/magento-admin-details/

+0

這與Dalton的答案基本相同,它不能解決切換會話的問題。 – Melanie

0

這很簡單,但不是推薦的解決方案。我自己花了數小時來做到這一點。 因爲,基於Windows Server嘗試以下解決方案:

$sessionFilePath = Mage::getBaseDir('session').DS.'sess_'.$_COOKIE['adminhtml']; 
$sessionFile  = file_get_contents($sessionFilePath); 
$exp_cookie = explode(';',$sessionFile); 
if(count($exp_cookie) > 100) 
{ 
    return "login"; 
} 
return "expire";  

因爲,基於Linux的服務器嘗試以下解決方案:

$sessionFilePath = Mage::getBaseDir('session').DS.'sess_'.$_COOKIE['adminhtml']; 
$sessionFile  = file_get_contents($sessionFilePath); 
$exp_cookie = explode('--',$sessionFile) 
if(count($exp_cookie) > 10) 
{ 
    return "login"; 
} 
return "expire"; 

感謝, 卡希夫