I got one site to fix issues and noticed one very odd issue.

Below code is work well in DISABLE cache mode but not in ENABLE mode. If I enable cache then it only works on customer-account (customer/account) section but not other parts/pages of site.

Code snippet of path:


$objectManagerlogin = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManagerlogin->get('Magento\Customer\Model\Session');
$baseurl = $objectManagerlogin->get('Magento\Store\Model\StoreManagerInterface')->getStore(0)->getBaseUrl();
 if($customerSession->isLoggedIn()) {

      echo "Logout";

 }else { 
      echo "Login";

PS: I am aware that Object Manager should not be used and commands (static content, reindex, cacha clean, di compilation) ran completely.

  • please let me know if you have issue Commented Jun 8, 2017 at 5:45
  • You have to add this code in your block file Commented Jun 8, 2017 at 7:02
  • You cannot solve it via PHP while using FPC. Every dynamic part should be handled via KnockoutJS in Magento2. Commented Jun 10, 2017 at 15:30
  • I sorted it and will post the answer as well. thx
    – Jarnail S
    Commented Jun 12, 2017 at 3:50

1 Answer 1


You can use below way to get current customer login status,

public function __construct(
        \Magento\Framework\App\Http\Context $appContext
    ) {
        $this->appContext = $appContext;

    protected function isLoggedIn()
        return $this->appContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);           
  • How to call this in header.phtml file? and one more thing that WHY question code behave different in enabled-cache?
    – Jarnail S
    Commented Jun 9, 2017 at 5:00
  • $objectManagerlogin = \Magento\Framework\App\ObjectManager::getInstance(); $customerSession = $objectManagerlogin->get('Magento\Framework\App\Http\Context')->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH); if($customerSession->isLoggedIn()){//your code}else{//your code} try with direct code Commented Jun 9, 2017 at 5:03
  • Direct code is not working. Before that, I created a block and called that isLoggedIn but that also created problem. Can you please update question with that included creating block and calling so that i check what i missed?
    – Jarnail S
    Commented Jun 9, 2017 at 5:41

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.