2015-06-02 80 views
6

我使用下面這個代碼登錄並重定向到帳戶頁面從外部Magento的帳戶頁面:Magento的:登錄並重定向到

<?php 
include('store/app/Mage.php'); 
Mage::app(); 

if($_POST && $_POST['login']['username'] && $_POST['login']['password']){ 
    $email = $_POST['login']['username']; 
    $password = $_POST['login']['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; 

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

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

?> 

,然後從那裏我將AJAX請求文件,我使用此代碼:

if(data.success){ 
    window.location = "http://domain.com/store/customer/account/" 
} 

但它始終顯示用戶註銷,雖然我確實得到正確的客戶ID以及成功。

回答

1
$email = strip_tags($_GET["login"]); 
$password = strip_tags($_GET["psw"]); 

function loginUser($email, $password) { 

    umask(0); 
    ob_start(); 
    session_start(); 
    Mage::app('default'); 
    Mage::getSingleton("core/session", array("name" => "frontend")); 

    $websiteId = Mage::app()->getWebsite()->getId(); 
    $store = Mage::app()->getStore(); 
    $customer = Mage::getModel("customer/customer"); 
    $customer->website_id = $websiteId; 
    $customer->setStore($store); 
    try { 
     $customer->loadByEmail($email); 
     $session = Mage::getSingleton('customer/session')->setCustomerAsLoggedIn($customer); 
     if($session->login($email, $password)){ return true;} else { }; 
    }catch(Exception $e){ 
     return $e->getMessage(); 
    } 


     } 



if (loginUser($email,$password) == 1) { 
echo ".. user loged as ".Mage::getSingleton('customer/session')->getCustomer()->getName()."<br>";} else { 
//bad things goes here 
} 
+0

你能告訴我我的代碼中缺少什麼嗎?因爲我能夠獲取客戶名稱,但仍然在我訪問商店時顯示用戶仍然未登錄。 – atif

+0

我認爲您沒有完全登錄,只調用會話存儲信息(創建瀏覽器登錄會話並存儲在magento會話)有更多的原因。首先你必須使用會話開始。其次,你必須設置存儲並且必須有管理會話,加載magento核心會話控制器。等..只是使用功能在我的答案,它會工作... – Martin

+0

我確實收到消息「..用戶loged作爲用戶名」,但仍然當我試圖訪問商店帳戶頁面,它將我重定向到登錄頁面。 – atif

1

在我的情況下馬丁的代碼的工作,如果我改變會話名稱

session_name('frontend'); 
session_start(); 

如果離開會話名稱單獨它默認PHPSESSID這是不一樣的,通過Magento的上手動創建登錄,並沒有在我的安裝工作。這可能會有所不同,請嘗試手動登錄並檢查您的Cookie名稱。

session_name文檔:http://php.net/manual/en/function.session-name.php