0

I am trying to implement a "Log In/Log Out" link like in the top menu but on a page outside of 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.

<?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?

lukaspanic
  • 31
  • 1
  • 1
  • 5

3 Answers3

2

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;
}
Elavarasan
  • 2,579
  • 2
  • 25
  • 42
  • @lukaspanic It works with me if I put `login.php` file inside the **magento root directory**. And the same code doesn't work outside it! **NOTE:** Do change **require_once** path while changing file location! – Harsh Goswami Jan 29 '14 at 06:29
  • I used Elavarasan solution, but i have to put the store code in the app-call - for example: Mage::app('default'); Without it does not work. – MoppieMop Feb 13 '14 at 14:15
1

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

huzefam
  • 1,201
  • 1
  • 12
  • 21
0

The above solutions worked for me in Firefox, but not 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)