【问题标题】:Magento API : How to load customer session by IDMagento API:如何按 ID 加载客户会话
【发布时间】:2015-02-16 16:28:43
【问题描述】:

我正在开发 magento 模块以使用 API 登录客户。

我可以登录客户并获取前端 sessionId, 但是当我想用这个 sessionId 加载会话来检查客户是否已经登录时,我不能。

这里是我使用的API函数:

public function login($email,$password,$storecode){
    $result = array();
    $result['code'] = '';
    $result['messages'] =  '';
    try{
        $session = Mage::getSingleton('customers/session');
        $storemodel = Mage::getModel('core/store')->load($storecode);
        $store_id = $storemodel->getId();
        $websiteId = $storemodel->getWebsiteId();
        if($session->loginByApi($email, $password,$websiteId)){
            $result['code'] = 'SUCCESS';
            $result['sessionId'] = $session->getSessionId();
            $customer = $session->getCustomer();
            $result['customer']= array(
                'customerid' => $customer->getId(),
                'firstname' => $customer->getFirstname(),
                'lastname' => $customer->getLastname(),
            );
        }
    } catch (Mage_Core_Exception $e) {
        $result['code'] = 'ERRORS';
        $result['messages'] =  $e->getMessage();
    }
    return $result;
}

public function isloggedin($customerId,$customersessionId ,$storecode){
    if(!isset($storecode)){
        $storecode = 'default';
    }

    Mage::app($storecode, 'store');

    $core_session = Mage::getSingleton('core/session', array('name'=>'frontend'));
    if($customersessionId != null){
        $core_session->setSessionId($customersessionId);
        $core_session->start('frontend');
    }

    $session = Mage::getSingleton('customer/session', array('name'=>'frontend'));

    $customer = Mage::getModel('customer/customer')->load($customerId);
    $session->setCustomer($customer);


   if($session->isLoggedIn()){
        $session->setCustomerAsLoggedIn($customer);
        $result['sessionId'] = $session->getSessionId();
    }else{
        $result['logged'] = false;
    }
    return $result;

}

有人有想法吗?

【问题讨论】:

  • 当你说“不能”时,你到底是什么意思? $session 为空,类似吗?
  • $session 被加载为访客而非客户登录,我总是使用 $session->isLoggedIn() 方法得到错误
  • 任何检查客户是否从会话 id 登录的解决方案?

标签: php api magento


【解决方案1】:

不确定这是否有太大帮助,但这段代码:

Mage::app('2', 'store');
$s = Mage::getSingleton('customer/session', array('name'=>'frontend'));
$c = Mage::getModel('customer/customer')->load(1);
$s->setCustomer($c);

if($s->isLoggedIn()){
    echo $c->getName()." is logged in, session: ".$s->getSessionId().PHP_EOL;
} else {
    echo "not logged in".PHP_EOL;
}

似乎对我有用:

John Smith is logged in, session: d3rcgvd56md4u3cfctcvnt2ou6

【讨论】:

  • 迈克,就我而言,我不知道客户 ID。我想通过使用 sessionID 加载会话来获得它
【解决方案2】:

如果您有会话 ID,您可以通过以下调用核心会话单例来获取数据:

$sessId = 'sn1q4ndvr1kieumsmplhd39n83';
$sess = Mage::getSingleton('core/session', array( 'value' => $sessId ));

这将为任何用户检索会话,无论是否登录。从那里您可以通过检查客户对象来确定会话是否属于客户:

$sessionCustomerId = $sess->getCustomer()->getId();

if ( $sessionCustomerId ) {
    // yay, they are a customer!
    echo $sessionCustomerId;
}

希望对您有所帮助。


编辑

您可以使用Mage::getSingleton('core/session')->getEncryptedSessionId()从核心会话中获取会话ID(以先有鸡的方式)

【讨论】:

  • 参考$sessId,这是从什么推导出来的?前端 cookie 值?因为 (frustratingly) 这对我不起作用。 $sess->getCustomer() 变为 NULL,我知道从检查员那里复制的值是正确的...
  • @Jester sn-ps 假设您已经拥有会话 ID(注意第 1 行)。您可以从不同的地方获取 id,很可能是通过遍历 var/session 文件夹并删除 sess_ 前缀。
  • 使用 'value' 参数很好,但只有在会话尚未初始化时才有效。
  • 错误代码:if ( $sessionCustomerId = $sess->getCustomer()->getId() ) 违反规则:条件赋值
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-13
  • 1970-01-01
  • 1970-01-01
  • 2012-04-16
  • 1970-01-01
  • 2012-12-14
相关资源
最近更新 更多