简体   繁体   English

如何从Magento外部访问Magento用户的会话?

[英]How to access Magento user's session from outside Magento?

Im trying to access the Magento customer session in another part of my website. 我试图访问我网站另一部分的Magento客户会话。

domain.com/shop/ <- Magento
domain.com/test.php

The shop itself works like a charm, however im trying to determine within test.php if a customer is logged in, so I can display his name with a link to his cart. 商店本身就像一个魅力,但我试图在test.php中确定一个客户是否登录,所以我可以显示他的名字与他的购物车的链接。

Contents of test.php so far: 目前test.php的内容:

<?php
require_once dirname(__FILE__).'/shop/app/Mage.php';
umask(0);
Mage::app('default');
Mage::getSingleton('core/session', array('name' => 'frontend'));
var_dump(Mage::getSingleton('customer/session')->isLoggedIn());
?>

I keep getting bool(false) returned. 我一直得到布尔(假)返回。 I'm logged into Magento at domain.com/shop/ so 'true' is expected. 我在domain.com/shop/登录了Magento,所以预计“真实”。

Am I forgetting something? 我忘记了什么吗?

I would recommend checking the path of the cookie that Magento sets. 我建议检查Magento设置的cookie的路径。 Most likely, it is set to /shop , so the cookie will not be accessible to your file above it. 最有可能的是,它设置为/shop ,因此上面的文件无法访问cookie。

You can modify the path that Magento uses to set its cookie in the control panel under System -> Configuration -> Web (under the General heading) -> Session cookie management 您可以在System -> Configuration -> Web (under the General heading) -> Session cookie management修改Magento用于在控制面板中设置其cookie的路径

The same issue was driving me mad. 同样的问题让我很生气。 I worked through the following until the last item solved it: 我完成了以下工作直到最后一项解决了它:

  • Has the correct Mage store ID (current store) been set? 是否设置了正确的Mage商店ID(当前商店)?
  • Are you using the same session path as Magento? 您使用的是与Magento相同的会话路径吗?
  • Are you using the same (sub)domain for cookie purposes? 您是否使用相同(子)域进行cookie目的?
  • Are you using HTTP or HTTPS both inside and outside Magento? 您是在Magento内外使用HTTP还是HTTPS?

If you've checked all the above, make sure you initialize a core "frontend" session on its own first like this: 如果您已经检查了以上所有内容,请确保首先自行初始化核心“前端”会话,如下所示:

// Initialise the core "frontend" session
Mage::getModel('core/session', array('name' => 'frontend'));

Then you can access the customer/session like this: 然后您可以像这样访问客户/会话:

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

it is under confuguration-> web on Magento ver. 这是在confuguration-> web ver的confuguration-> web 1.10.1.1 1.10.1.1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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