3

I have the following code to see if a user is logged in. It sort of works as in it works within the customer area (user logged in) but doesn't work outside of the customer area even though the customer is still logged in.

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
if($customerSession->isLoggedIn()) { ?>
   <li class="link wishlist" data-bind="scope: 'wishlist'">
    <a <?php /* @escapeNotVerified */ echo $block->getLinkAttributes() ?>><?php echo $block->escapeHtml($block->getLabel()) ?>
        <!-- ko if: wishlist().counter -->
        <span data-bind="text: wishlist().counter" class="counter qty"></span>
        <!-- /ko -->
    </a>
</li>
<li>Hello World</li>
?>
<?php
}
else {
?>
    <li>Not logged in</li>
<?php
}
?>



<script type="text/x-magento-init">
    {
        "*": {
            "Magento_Ui/js/core/app": {
                "components": {
                    "wishlist": {
                        "component": "Magento_Wishlist/js/view/wishlist"
                    }
                }
            }
        }
    }
</script>
1
  • 2
    which version of Magento? Commented Apr 19, 2016 at 16:21

7 Answers 7

7

In magento 1.9, if you want to check if the user is logged in any controller or phtml, you need to add

<?php 
    if( ! Mage::getSingleton('customer/session')->isLoggedIn()){
        //not logged in
    }else{
        // logged in
    }
?>

the important part to intance the super object is this

 Mage::getSingleton('customer/session')
Sign up to request clarification or add additional context in comments.

1 Comment

Take a look at the code he's using. This is Magento 2, not Magento 1.9. He should've specified that though.
2

In Magento 2.x PHP check if user is logged in (phtml file), you can use below code:

     $authorizationLink = $block->getLayout()->createBlock('Magento\Customer\Block\Account\AuthorizationLink');

    <?php if($authorizationLink->isLoggedIn()){
        // Customer is logged In
     }else{
        // Customer is not logged In
    } 
?>

1 Comment

not sure why but session wasn't working in 2.3.5 and this way works just fine
1

It's because of echo $block->getLinkAttributes()

This is block is vendor\magento\framework\View\Element\Html\Link.php and it is not called as $block on every page , so if you need getLinkAttributes() you need to call it manually.

Comments

0

Check Customer is Logged In or not in Whole Website

$om = \Magento\Framework\App\ObjectManager::getInstance();
/** @var \Magento\Framework\App\Http\Context $context */
$context = $om->get('Magento\Framework\App\Http\Context');
/** @var bool $isLoggedIn */
$isLoggedIn = $context->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);

     if($isLoggedIn == 1){
     //Customer is Logged In
     }

2 Comments

how to get customer name in this class
objectManager is a dirty way
0

Use the following code to check whether the user is logged in or not:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');

if($customerSession->isLoggedIn()) 
{
 // your code
}

2 Comments

Can confirm this works with Magento 2.1: this is perfect thanks.
Should not use objectManager
0

You should not listen to all the users telling you to use objectManager. This is dirty and less performant. And you should always keep the logic in your block class instead of writing it in your template. Always separate buisness logic from the view.

The right way:

For this example I add a new method "isUserLoggedIn()" to the block class \Magento\Checkout\Block\Cart\Sidebar.

First I extend the class with my own Custom\Module\Block\Cart\Sidebar .

<?php

namespace Custom\Module\Block\Cart;

class Sidebar extends \Magento\Checkout\Block\Cart\Sidebar
{
    private $customerSession;
    ...

    public function __construct(
        \Magento\Checkout\Model\Session $checkoutSession,   
        ...
    ) {

        $this->customerSession = $customerSession;
        ...
    }

    public function isLoggedIn()
    {
        return $this->customerSession->isLoggedIn();
    }

Now you can use $block->isLoggedIn() in your template.

Comments

-1

Use the following code to resolve your query.

<?php
$objectManagerlogin = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManagerlogin->get('Magento\Customer\Model\Session');
$baseurl = $objectManagerlogin->get('Magento\Store\Model\StoreManagerInterface')->getStore(0)->getBaseUrl();
?>
<?php if($customerSession->isLoggedIn()) { ?>
  <a href="<?php echo $baseurl .'customer/account/logout'; ?>">LOGOUT</a>
<?php }else { ?>
<a href="<?php echo $baseurl .'customer/account/login'; ?>">LOGIN</a>    
<?php
}
?>

1 Comment

Thats dirty, you should never use objectManager. And you have to add code in the block class and then just call it in the template

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.