[英]Magento Check if customer is logged in from outside of magento
I am trying to implement a "Log In/Log Out" link like in the top menu but on a page outside of magento. 我试图在顶部菜单中但在magento之外的页面上实现“登录/注销”链接。 Here is what I tried so far: Instead of a plain "LogIn" link I used this script to load Mage and then show the link depending on the customer being logged in or not.
到目前为止,这是我尝试的方法:我不是使用简单的“ LogIn”链接而是使用此脚本加载Mage,然后根据是否登录的客户显示该链接。
<?php
require_once('tmg/app/Mage.php'); //Path to Magento
umask(0);
Mage::app();
?>
<?php if (Mage::getSingleton('customer/session')->isLoggedIn()==0): ?>
<a href="<?php echo $this->getUrl('customer/account/login') ?>"><?php echo $this- >__('Log In') ?></a>
<?php else: ?>
<a href="<?php echo $this->getUrl('customer/account/logout') ?>"><?php echo $this->__('Log Out') ?></a>
<?php endif ?>
What is being displayed in the browser is the page up to this code and absolutely nothing after that. 在浏览器中显示的是直至此代码的页面,此后绝对没有任何内容。 I also tried to put this into a test file by itself and run it but it also results in an empty browser window, no source code or anything visible.
我还尝试将其单独放入测试文件中并运行它,但它也导致浏览器窗口为空,没有源代码或任何可见内容。 What am I doing wrong?
我究竟做错了什么?
Try this.. 尝试这个..
require_once('tmg/app/Mage.php');
umask(0);
Mage::app();
//GET SESSION DATA
Mage::getSingleton('core/session', array('name'=>'frontend'));
$session = Mage::getSingleton('customer/session', array('name'=>'frontend'));
$customer_data = Mage::getModel('customer/customer')->$session->id;
//CHECK IF LOGGED IN
if($session->isLoggedIn()){
?>
<a href="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);?>customer/account/logout"><?php echo "Log Out"; ?></a>
<?php
} else {
?>
<a href="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);?>customer/account/login"><?php echo "Log in"; ?></a>
<?php
exit;
}
Do check if this helps! 请检查是否有帮助!
require_once 'app/Mage.php';
umask(0);
Mage::app('default');
Mage::getSingleton('core/session', array('name' => 'frontend'));
$sessionCustomer = Mage::getSingleton("customer/session");
if($sessionCustomer->isLoggedIn()) {
echo "Logged";
} else {
echo "Not Logged";
}
for more details check here Magento Customer login 欲了解更多详情,请点击这里Magento客户登录
The above solutions worked for me in Firefox, but not Chrome. 以上解决方案对我来说适用于Firefox,但不适用于Chrome。 I had to add
我必须添加
Mage::getSingleton('customer/session')->start();
after the line 下线后
Mage::app('default');
Then it appears to work fine. 然后,它似乎可以正常工作。 (magento 1.9)
(magento 1.9)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.