跨子域的 PHP 会话 [英] PHP Sessions across sub domains

查看:32
本文介绍了跨子域的 PHP 会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置以下内容:

I am trying to set up the following:

auth.example.com
sub1.example.com
sub2.example.com

如果用户访问 sub1.example.comsub2.example.com 并且他们没有登录,他们会被重定向到 auth.example.com 并可以登录.

If the user visits sub1.example.com or sub2.example.com and they are not logged in, they get redirected over to auth.example.com and can log in.

sub1.example.comsub2.example.com 是两个独立的应用程序,但使用相同的凭据.

sub1.example.com and sub2.example.com are two separate applications but use the same credentials.

我尝试在我的 php.ini 中设置以下内容:

I tried setting the following in my php.ini:

session.cookie_domain = ".example.com"

但它似乎没有将信息从一个域传递到另一个域.

but it doesn't seem to be passing the information from one domain to the other.

我尝试了以下方法:

sub1.example.com/test.php

session_set_cookie_params(0, '/', '.example.com');
session_start();
print session_id() . "<br>";
$_SESSION['Regsitered'] = 1;
echo '<a href="http://auth.example.com/test.php">Change Sites</a>'

auth.example.com/test.php

session_set_cookie_params(0, '/', '.example.com');
session_start();
print session_id() . "<br>";
$_SESSION['Checked'] = 1;
print_r($_SESSION);

会话 ID 完全相同,但是当我转储 $_SESSION 变量时,它不会显示两个键,只是我在每个域下设置的任何键.

The session IDs are exactly the same but when I dump out the $_SESSION variable it doesn't show both keys, just whatever key I set under each domain.

推荐答案

我不知道问题是否仍然存在,但我遇到了同样的问题并解决了它在调用 session_set_cookie_params() 之前设置会话名称:

I do not know if the problem still exists, but I just ran into the same problem and solved it setting a session name before calling session_set_cookie_params():

$some_name = session_name("some_name");
session_set_cookie_params(0, '/', '.example.com');
session_start();

我在 php.ini 中没有做任何更改,但现在一切正常.

I have changed nothing in my php.ini but now everything is working fine.

这篇关于跨子域的 PHP 会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆