Magento PHP check if user is logged in
<p>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.</p>

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



&lt;script type="text/x-magento-init"&gt;
    {
        "*": {
            "Magento_Ui/js/core/app": {
                "components": {
                    "wishlist": {
                        "component": "Magento_Wishlist/js/view/wishlist"
                    }
                }
            }
        }
    }
&lt;/script&gt;
</code></pre>