3

我正在尝试以客户身份登录。我请求以下代码 (AJAX) 和这个外部 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/session 目录中成功创建会话文件,但无法在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 表中写入会话条目。

4

5 回答 5

1

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

Mage::getSingleton('customer/session')->loginById($customerId);
于 2013-02-25T14:45:20.007 回答
0

商店ID和网站ID在这里设置自定义你会根据你改变

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


}

?>
于 2014-02-26T19:04:56.283 回答
0

无需通过 GSD Ajax 登录加载任何页面即可轻松打开您的 Magento 帐户并忘记密码。下面的网址非常有用。

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

于 2014-09-17T06:32:12.017 回答
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);
于 2013-05-28T06:39:06.187 回答
0

在控制器中使用以下代码magento它将在控制器内工作,因为自定义PHP文件无法生成magento访问者日志

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;


  }
于 2021-07-15T10:53:08.100 回答