2013-02-25 121 views
3

我想作爲客戶登錄。 我在請求下面的代碼(AJAX)和這個外部的magento商店。Magento客戶登錄從外部magento

<?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); 
?> 

我發現上面的代碼在成功VAR /會話目錄創建會話文件,但不能寫在log_customer DB表客戶准入。任何人都知道這裏有什麼問題。 謝謝:)

修訂 Okie以下更新的代碼(customerLogin.php)工作條件

<?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); 
?> 

,如果我喜歡跟着下目錄結構:

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

但它不工作,如果我把代碼放在一個目錄級別下:

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

任何人都知道最新的問題。 我想知道爲什麼magento無法在log_customer表中寫入會話條目,當我將我的代碼放在一個層次下。

回答

1

獲取您的客戶ID然後使用下面的代碼。

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

試試這段代碼從magento外部登錄,在magento根目錄下創建demo.php文件,並輸入以下代碼。

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); 
+0

我用你的代碼,它給我的成功以及客戶ID,但是當我嘗試導航到佔商店的頁面,它重定向我到登錄頁面。任何想法? – atif 2015-06-02 19:20:35

0

店鋪ID和網站ID在這裏設置自定義 你會改變按照到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 = "[email protected]"; 

$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'; 


} 

?>