cancel
Showing results for 
Search instead for 
Did you mean: 

Check to see if user is logged in on external php page

SOLVED

Check to see if user is logged in on external php page

I would like to display the user's name if logged in or a link to login if not on a php page outside of Magento. They are all within the same domain. I'm using CE2.2.

Thanks,

Mike

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Check to see if user is logged in on external php page

Hi @MIKE_JF,

You can't access logged customer info outside the magento folder. It is against the magento security standards.

If you want to create a simple php file inside the magento root folder then you can access by following way.

<?php

ini_set('display_errors', 1);
error_reporting(E_ALL);

use Magento\Framework\App\Bootstrap;
require 'app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);

$objectManager = $bootstrap->getObjectManager();

$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$customerSession = $objectManager->create('Magento\Customer\Model\Session');
if($customerSession->isLoggedIn()) {
	echo $customerSession->getCustomer()->getName();
}

I hope it will help you!

View solution in original post

4 REPLIES 4

Re: Check to see if user is logged in on external php page

Hi @MIKE_JF,

On which page you want to show customer name outside of magento.

Is it php page or html or any else? Then I can suggest you better solution.

Re: Check to see if user is logged in on external php page

Hi Vimal,

It is a php page in the root folder of the website. The Magento installation resides in the /store sub directory.

Thanks,

Mike

Re: Check to see if user is logged in on external php page

Hi @MIKE_JF,

You can't access logged customer info outside the magento folder. It is against the magento security standards.

If you want to create a simple php file inside the magento root folder then you can access by following way.

<?php

ini_set('display_errors', 1);
error_reporting(E_ALL);

use Magento\Framework\App\Bootstrap;
require 'app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);

$objectManager = $bootstrap->getObjectManager();

$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$customerSession = $objectManager->create('Magento\Customer\Model\Session');
if($customerSession->isLoggedIn()) {
	echo $customerSession->getCustomer()->getName();
}

I hope it will help you!

Re: Check to see if user is logged in on external php page

Thank you!