Magento中如何獲取/判斷用戶登錄狀態

開發人員直接用

Mage::getSingleton('customer/session')->isLoggedIn()

來判斷用戶是否登錄

比如一般magento開發人員會這樣用

<?PHP

//get customer login status ?>

<?php $myStatus = Mage::getSingleton('customer/session')->isLoggedIn() ?>

<?php if($myStatus): ?>

<li><a href="/customer/account/index" title="Customer Register">My account</a> |</li>
<li><?php echo $this->getLayout()->getBlock('header')->getWelcome() ?></li>

<?php else: ?>

<li><a href="/customer/account/index" title="Customer Register">My account</a></li>
<li><a href="/customer/account/create" title="Customer Register">Register</a></li>

<?php endif ?>

但其實在magento裏面用戶登錄狀態判斷函數早已封裝好了.
判斷用戶登陸狀態是否登陸的原理是:Magento在Session中檢查CustomerID是否已經設置,並且該CustomerID在數據庫中是有效的。

在app/code/core/Mage/Customer/Helper/Data.php文件中

/**
     * Check customer is logged in
     *
     * @return bool
     */
    public function isLoggedIn()
    {
        return Mage::getSingleton('customer/session')->isLoggedIn();
    }

在app/code/core/Mage/Customer/Model/Session.php文件中

/**
     * Checking customer login status
     *
     * @return bool
     */
    public function isLoggedIn()
    {
        return (bool)$this->getId() && (bool)$this->checkCustomerId($this->getId());
    }

所以我們可以在全局用

if ($this->helper('customer')->isLoggedIn()) { 
  // is logon 
 } 

在magento中判斷用戶的登錄狀態或是否登錄

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章