使用PHP跨域共享会话数据


Sharing session data across domains with PHP

我的用户登录到CMS的site_url.com,但我现在正在CMS之外的子域m.site_url.com上开发该网站的移动版本,并希望会话继续进行。

我已经包含了一个具有所有CMS功能的文件,如下所示:

<?php include_once'/home/flyeurov/public_html/core/codon.config.php';?>

我可以在移动网站上很好地登录:

<input type="hidden" name="mobileVersion" value="True">
<input type="hidden" name="redir" value="" />
<input type="hidden" name="action" value="login" />
<input class="login-btn" type="submit" name="submit" value="Log In" />

以上内容由login.php处理。然而,CMS登录会将其重定向到CMS的页面,我需要移动版本重定向到/mobile目录中m.site_url.com子域指向的页面。因此,在login.php中(不要与mobile/login.php混淆——主login.php用于CMS)。我这样做了,它重定向到mobile/crew_center.php——它应该在移动版本上重定向到这个页面。

if (isset($_POST['mobileVersion'])) {
                header('Location: http://m.site_url.com/crew_center.php');
            } else {
                $this->post->redir = str_replace('index.php/', '', $this->post->redir);
                header('Location: '.url('/'.$this->post->redir));
            }

也许上面的代码与我的问题有关。

我的问题是,主CMS的会话没有转移到移动站点。在mobile/index.php中,我有这样的语句,但它不起作用。登录时,用户应该看到crew_center.php,因为显示登录表单是没有意义的。目前,当用户登录CMS时,登录页面仍然出现在移动版上。

但是,如果我在浏览器中放下子域并键入site_url/mobile,它将打开右侧页面crew_center.php

<?php include_once'/home/flyeurov/public_html/core/codon.config.php';
session_set_cookie_params(0, '/', 'site_url.com');
session_start();
if(Auth::LoggedIn())
{
    header("Location: crew_center.php");
} 
else
{
    header("Location: login.php");
}
?>

如何修复此问题,以及如何使用PHP将会话数据从主域共享到子域?


codon.config.php-http://pastebin.com/BWcbddGG

您需要相应地设置cookie的域-在域名中添加一个.

session_set_cookie_params(0, '/', '.site_url.com');

这将在您的所有子域和二级域中共享您的cookie。