Magento – How to Create session for customer login with session outside magento

magento-1.7magento-connect

I am building a small web services code to where I need to have Magento customer login.

But I am not able to create the session. If I create the session from core/session it remains same for all users.

Question 1: Where are the sessions stored in the database?

Question 2: How to create a customer session (customer/session) and then fetch that on different locations with the same session ID??

Best Answer

Include Mage.php from Magento in your external application

// Include Magento application
require_once ( "/your-magento-directory/app/Mage.php" );
umask(0);
     
// Initialize Magento
Mage::app("default");
     

You have two options then, for the session name, "frontend" for frontend session or "adminhtml" for admin session:

Mage::getSingleton("core/session", array("name" => "frontend"));
$session = Mage::getSingleton("customer/session");

This will get the session of the current logged in user.

And this will login to Magento from outside:

$session = Mage::getSingleton('customer/session', array('name' => 'frontend'));
$session->login($row['mail'], $row['field_magentopass_value']);
$session->setCustomerAsLoggedIn($session->getCustomer());

$row['mail'] is the email with user is registered $row['field_magentopass_value'] is the password

Source: http://louisjohn.wordpress.com/category/magento/magento-functions-outside-magento-folder/