.

.

Login by email & password in magento programmattically

To login a customer into a given website by email and password we must obtain a session object on which we have to call the login()-method. But before that we have to initialize the correct application object. This means that the first parameter to pass to Mage::app() or Mage::init() is the website ID of the customer to login and the second one is the string "website".

<?php
require_once 'app/Mage.php';
 ?>
 <?php 
    Mage::app('default');    
    Mage::getSingleton("core/session", array("name" => "frontend"));
 $data=array();
 $email = str_replace("'","`",$_REQUEST['emailid']);
    $password = str_replace("'","`",$_REQUEST['password']);   
    $websiteId = Mage::app()->getWebsite()->getId();
    $store = Mage::app()->getStore();
    $customer = Mage::getModel("customer/customer");
    $customer->website_id = $websiteId;
    $customer->setStore($store);
 
//$jSon = ' {"login_data":[ ';

if (!empty($email) && !empty($password )) 
{
    try {
        $customer->loadByEmail($email);
        $session = Mage::getSingleton('customer/session')->setCustomerAsLoggedIn($customer);
        $session->login($email, $password);  
        $data['responseCode']='1';
        $data['msg']='successful';
        $data['result']=array("customerId"=>$session->getCustomer()->getId(),"customerFirstName"=>$session->getCustomer()->getFirstname(),"customerLastName"=>$session->getCustomer()->getLastname(),createdAt=>$session->getCustomer()->getCreatedAt(),"customerEmail"=>$session->getCustomer()->getEmail(),"sessionId"=>$session->getCustomer()->getId());
    } 
 catch(Exception $e){
 $data['responseCode']="0";
 $data['msg']=$e->getMessage();
     // $jSon .= ' {"message":"Email or password Invalid."}'; 
    }

}
else 
{
$data['responseCode']="0";
$data['msg']="Email and password are required";
       
}
$data2 = json_encode($data);
echo $data2;


?>


EmoticonEmoticon