J Norton Freelance Web Developer and Designer Logo
Website developer and designer providing WordPress
and Magento Commerce solutions tailored to your needs.

Blog

Feb
04

Magento: Check if Customer Logged in

Fri, 02/04/2011 - 10:31

Magento offers a simple way to include the application from an external script but getting the customer session data into your external page can be a real pain. This simple tutorial gives you the information you need to use Magento customer session data in an external script hosted outside of the core Magento folder.

I am currently working on some bespoke web applications and services using Magento as firewall to assess if a user is both logged in and if the user has paid for a particular item.

This means that I can offer paid for access to web applications and VIP areas of websites.

Take a look at the following script:

//LOAD MAGENTO
require_once 'YOUR_PATH_TO_MAGENTO/app/Mage.php';
umask(0);
Mage::app('YOUR_WEBSITE_CODE', 'website');

//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()){
echo 'Welcome ' . $customer_data->firstname . " " . $customer_data->lastname;
} else {
echo "Access Denied: Sorry, but this page is for registered members only.";
exit;
}

The code is very simple but there are 2 very important things to be aware of.

  1. Mage::app('YOUR_WEBSITE_CODE', 'website') - Make sure you have defined a scope for Magento to use.
  2. Mage::getSingleton('core/session', array('name'=>'frontend')) - Make sure you have called the core session data first before attempting to use the customer session.

Following these steps you should now have the ability of providing restricted access to your web applications and VIP pages.

3 Comments so far | Add new comment

#1 | Sun, 09/18/2011 - 09:52
default user picture
  • By: Olivier

Hello,

What do you mean by "Mage::app('YOUR_WEBSITE_CODE', 'website') - Make sure you have defined a scope for Magento to use." ?

i.e. what should I put instead of YOUR_WEBSITE_CODE in the call to Mage::app?

thank you very much

Olivier

#2 | Tue, 10/11/2011 - 19:29
default user picture
  • By: J Norton

Hello Olivier,

When you create a website in Magento at System > Manage Stores and by clicking 'Create Website' you will give the a code or identifier. So YOUR_WEBSITE_CODE is simply the code you assign to the website within the Magento admin interface.

I hope that helps.

#3 | Wed, 03/21/2012 - 11:51
default user picture
  • By: Magefast

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()) {} else {die();}

$customerGroupId = $sessionCustomer->getCustomerGroupId();

$customerGroupName = Mage::getModel('customer/group')->load($customerGroupId)->getCode();

//echo $customerGroupName;