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里面用户登录状态判断函数早已封装好了. 在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 }(责任编辑:好模板) |