简体   繁体   中英

Magento customer login from outside magento

I am trying to login as a customer. I am requesting the Following code (AJAX) and this outside magento store.

<?php
require_once "../app/Mage.php"; 
umask(0);
Mage::app('default');
Mage::getSingleton("core/session", array("name" => "frontend"));
$user = $_POST['user'];
$password = $_POST['password'];
$session = Mage::getSingleton('customer/session');
$flag = 0;  
$resultArr = array();
function customerLogin($user,$password){
    try{        
        $session = Mage::getSingleton('customer/session');
        $result = $session->login($user,$password);
        $customer = $session->getCustomer();
        $session->setCustomerAsLoggedIn($customer);

        $resultArr['flag'] = 1;
        $resultArr['msg'] ='Logged in as '.$customer->getName();
        $jsonReturn = json_encode($resultArr);
        return $jsonReturn;
    }catch(Exception $e){
        $resultArr['flag'] = 0;
        $resultArr['msg'] = $e->getMessage();   
        $jsonReturn = json_encode($resultArr);
        return $jsonReturn;
    }
}
echo customerLogin($user,$password);
?>

I found the above code is Creating the session files at var/session directory successfully but unable to write customer entry in log_customer DB table . Anybody know whats the problem here. Thanks :)

UPDATED Okie the following updated code(customerLogin.php) is working under a condition

<?php
function customerLogin($user,$password){
    require_once "./app/Mage.php"; 
    Mage::app('default');
    Mage::getSingleton("core/session", array("name" => "frontend"));
    $user = $_POST['user'];
    $password = $_POST['password'];
    $flag = 0;  
    $resultArr = array();
    $session = Mage::getSingleton('customer/session');
    try{        
        $result = $session->login($user,$password);
        //$customer = $session->getCustomer();      
        $session->setCustomerAsLoggedIn($session->getCustomer());
        $resultArr['flag'] = 1;
        $resultArr['msg'] ='Logged in as '.$session->getCustomer()->getName();
        $cusArr = array(
              'isLoggedIn'  => true,
              'name'        => $session->getCustomer()->getName(),
              'id'          => $session->getId(),
              'email'       =>$session->getCustomer()->getEmail(),
          );
        $resultArr['customerData'] = $cusArr;   
        $jsonReturn = json_encode($resultArr);
        return $jsonReturn;
    }catch(Exception $e){
        $resultArr['flag'] = 0;
        $resultArr['msg'] = $e->getMessage();   
        $jsonReturn = json_encode($resultArr);
        return $jsonReturn;
    }
}

echo customerLogin($user,$password);
?>

if i follow the directory structure like:

|-- app
|-- **customerLogin.php**
|-- downloader
|-- install.php
|-- js
|-- lib
|-- media
|-- var

But its not working if I place code one directory level down like :

|-- app
|-- **customerLogin**
    `--**customerLogin.php**
|-- downloader
|-- install.php
|-- js
|-- lib
|-- media
|-- var

Anybody knows whats the problem. I am wondering why magento is unable to write session entry in log_customer table when i place my code one level down.

获取您的客户 ID,然后使用以下代码。

Mage::getSingleton('customer/session')->loginById($customerId);

try this code to login from outside of magento, create demo.php file in magento root and put following code.

include('app/Mage.php');
Mage::app();
    if($_POST && $_POST["email"] && $_POST["password"])
    {
            $email=$_POST["email"];
            $password=$_POST["password"];
            $session = Mage::getSingleton('customer/session');

            try {
                      $log = $session->login($email, $password);
                      $session->setCustomerAsLoggedIn($session->getCustomer());
                      $customer_id=$session->getCustomerId();

                      $send_data["success"]=true;
                      $send_data["message"]="Login Success";
                      $send_data["customer_id"]=$customer_id;
                } 

                catch (Exception $ex) {
                            $send_data["success"]=false;
                                $send_data["message"]=$ex->getMessage();
                            }
    }
    else
    {
            $send_data["success"]=false;
            $send_data["message"]="Enter both Email and Password";
    }
    echo json_encode($send_data);

store id and website id here set custom you will change accordance to u

    <?php 
ob_start();
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

require_once "app/Mage.php";

umask(0); 

Mage::app();


$firstname = "krishana";

$lastname = "singh";

$email = "krish.bhati@gmail.com";

$password = "12345678";


// Website and Store details

$websiteId = Mage::app()->getWebsite()->getId('1');



$store = Mage::app()->getStore('31');




$customer = Mage::getModel("customer/customer");

 $customer->website_id = $websiteId;

$customer->setStore($store);


$mageRunCode = isset ( $_SERVER ['MAGE_RUN_CODE'] ) ? $_SERVER ['MAGE_RUN_CODE'] : '';

$mageRunType = isset ( $_SERVER ['MAGE_RUN_TYPE'] ) ? $_SERVER ['MAGE_RUN_TYPE'] : 'store';

$app = Mage::app ( $mageRunCode, $mageRunType );


Mage::app('default');
Mage::getSingleton('core/session', array('name' => 'frontend')); 

$session = Mage::getSingleton('customer/session');

$session->start();

$customer->loadByEmail($email); 

 $customer_id= $customer->getId();



if($customer_id){   
Mage::getSingleton('customer/session')->loginById($customer_id);
Mage_Core_Model_Session_Abstract_Varien::start();

try{ 
    $session->login($email, $password);
   }
catch(Exception $e) {}

$session->setCustomerAsLoggedIn($session->getCustomer()); 

echo $session->isLoggedIn() ? $session->getCustomer()->getName().' is online!' : 'not logged in';


}

?>

Easily open your account & forgot password for your Magento without loading any pages through GSD Ajax login. Below URL is very useful.

http://www.magentocommerce.com/magento-connect/easy-ajax-login-by-smartshore.html

use below code in magento controller it will work inside controller because custom PHP file can not generate magento visitor log

public function customerLoginSession($customer_id){

        /** @var $session Mage_Customer_Model_Session */
        $session = Mage::getSingleton( 'customer/session' );
        Mage::app()->getStore()->setWebsiteId(1);
        try
        {
            //$session->login( $user, $pass);
            $session->loginById($customer_id);
            $customer = $session->getCustomer();
            $session->setCustomerAsLoggedIn($customer);

            $res = json_encode(array('status' => 'valid', 'userData' => $customer->getId()));
        }
        catch( Exception $e )
        {
            $res = json_encode(array('status' => 'invalid', 'userData' => $e->getMessage()));
        }
        return $res;


  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM