大部分开发人员直接用
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中判断用户的登录状态或是否登录