如何在 Magento 之外创建 Magento 会话?
我可以使用下面的流行方法完美地访问 Magento 之外的现有会话。
require 'app/Mage.php';
$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::getSingleton ( 'core/session', array ('name' => 'frontend' ) );
这很好用,但是我实际上如何在 Magento 之外创建一个 Magento 会话来填充 log_url、log_visitor 等表以及将访问者数据分配给会话?
目前,用户直接从另一个网站到达我网站上的页面。此特定页面位于 Magento 外部,但我需要使用以下代码访问他们的访客 ID:
Mage::getSingleton ( 'log/visitor' )->getId()
如果用户之前访问过我的 Magento 商店,则效果很好,但如果没有,它只会返回一个布尔值错误的。我想要做的是检查是否为访客 ID 设置了值,如果没有,请在 Magento 外部的第一页上创建访客,以便我可以在此页面上使用访客 ID。同样重要的是,一旦用户进入我的 Magento 商店,相同的访客 ID 将应用于我的目录的整个导航,即相同的会话。有什么想法吗?
I am able to access an existing session outside of Magento perfectly fine using the popular method below.
require 'app/Mage.php';
$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::getSingleton ( 'core/session', array ('name' => 'frontend' ) );
This works great, but how do I actually create a Magento session outside of Magento that would populate the log_url, log_visitor, etc. tables as well as assigning the visitor data to the session?
Currently, the user arrives at a page on my site directly from another website. This specific page is outside of Magento but I need to access their Visitor ID using the following code:
Mage::getSingleton ( 'log/visitor' )->getId()
This works fine if the user has previously been to my Magento store, but if not, it just returns a boolean false. What I would like to do is check to see if there is a value set for the Visitor ID and if not, create the visitor on this first page which is outside of Magento so I can use the Visitor ID right on this page. It is also important that as soon as the user enters my Magento store, the same Visitor ID will be applied throughout their navigation of my catalog, i.e. the same session. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
嗯,我已经弄清楚了。尽管我必须承认这不是最干净的解决方案,但它的工作原理完全符合我的希望。对于其他想要这样做的人,我已将代码摘录粘贴在下面:
我希望这对其他人有帮助,这样他们就不会像我一样抓狂。
Well, I've figured it out. Although I must admit it's not the cleanest solution, it works exactly as I had hoped. For anyone else who's looking to do this, I've pasted my code excerpt below:
I hope this helps someone else so they're not ripping their hair out like I was.
不确定这是否有帮助,但这里有一些代码可以帮助通过 app() 引导方法更准确地加载 magento。通过分派一些事件,它实例化访问者对象并加载一些其他有用的对象/资源,使其功能类似于本机 Magento 的 run():
Not sure if this helps, but here's a bit of code to help load magento a little more accurately via the app() bootstrap method. By dispatching some events, it instantiates the visitor object and loads some other helpful objects/resources that make it function similar to native Magento's run():
实际上这些答案不起作用,因为我正在 Magento 的子目录中运行代码。 Magento 会查看 SCRIPT_NAME 变量的目录名,并将其设置在那里。因此,如果您从 /foo/test.php 设置会话,那么 cookie 将仅对 /foo/ 有效,如果 magento 位于“/”,它将看不到 cookie。
在这种情况下,如果您的脚本是最初创建 cookie 的脚本,则必须强制 cookie 对“/”有效。
症状是您的更改没有生效,并且您可以在“/var/session”中看到两个会话。这似乎不可预测,但不同的是,如果 Magento 首先创建会话,则它对所有路径都有效。如果您的脚本首先创建它,则它对 Magento 无效,浏览器会忽略它,并且 Magento 会创建一个新会话。
示例工作代码:(
我使用 DOCUMENT_ROOT 的原因是因为我使用来自此模块的 modman 映射,它通过符号链接运行代码)。重要的部分是setcookie()。通过检查 http 标头,您会看到设置了两个 cookie。一种用于“/foo”,另一种用于“/”。如果不调用 setCookie,则仅创建第一个会话,当通过“/index.php”而不是“/foo/test.php”访问时,Magento 不可见该会话
Actually these answers didn't work because I was running code in a subdirectory of Magento. Magento looks at the dirname of your SCRIPT_NAME variable, and sets it there. So if you're setting the session from /foo/test.php then the cookie will only be valid for /foo/, if magento is at "/", it won't see the cookie.
In this case, if your script is the one initially creating the cookie, you have to force the cookie to be valid for "/".
The symptoms is that your changes don't take affect, and you can see two sessions in "/var/session". It seems unpredictable, but the different is if Magento creates the session first, its valid for all paths. If your script creates it first, its not valid for Magento, the browser omits it, and Magento creates a new session.
Example working code:
(The reason I used DOCUMENT_ROOT is because I'm using a modman mapping from this module, which runs the code through a symlink). The important part is the setcookie(). You'll see from examining the http headers two cookies are set. One for "/foo" and one for "/". Without the call to setCookie, only the first session is created, which isn't visible to Magento when accessed through "/index.php" instead of "/foo/test.php"
并
在 Magento 之外的 Magento 会话中工作
And
Working for Magento session outside of Magento