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中判断用户的登录状态或是否登录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值