简体   繁体   English

跨多个域共享php Session($ _SESSION)

[英]Sharing php Session ($_SESSION) across multiple domain

I have 2 different domain, let's call them www.foo.com and bar.foo.com. 我有2个不同的域名,我们称之为www.foo.com和bar.foo.com。 The first one is built with CI, and the second one is built with Symfony. 第一个是用CI构建的,第二个是用Symfony构建的。 I want to share my session, so if I login in one of them, I can access the other one. 我想分享我的会话,所以如果我登录其中一个,我可以访问另一个。 I set my session data with $_SESSION["session_name"] = "value"; 我用$_SESSION["session_name"] = "value";设置我的会话数据$_SESSION["session_name"] = "value"; .

How to make a session data readable from the other domain? 如何使会话数据可以从其他域读取?

Thanks for your help. 谢谢你的帮助。

Considering your original question and your answers to Logan and myself in the comments of the original question I understand: 在原始问题的评论中考虑到您的原始问题以及您对Logan和我自己的回答,我理解:

1 - you want to pass the session variables among a domain and its subdomains; 1 - 您希望在域及其子域之间传递会话变量; and

2 - CI and Symfony load the session before you have a chance to do the ini_set command. 2 - CI和Symfony在您有机会执行ini_set命令之前加载会话。

I believe you have two options: 我相信你有两个选择:

1 - include the php configuration command in the php.ini file 1 - 在php.ini文件中包含php配置命令

session.cookie_domain=".foo.com"

If you try including it in the .htaccess it will not work if you are running php as a CGI module, which seems to be fairly common among shared hosting services. 如果你尝试将它包含在.htaccess中,那么如果你将php作为CGI模块运行它将无法工作,这在共享主机服务中似乎相当普遍。

2 - you can prepend a file to all php scripts in your site. 2 - 您可以将文件添加到站点中的所有php脚本。 Those will be put on top of every single php script your site runs, even the ones inside CI and Symfony. 这些将被放在您的网站运行的每个PHP脚本之上,甚至是CI和Symfony中的那些。 For example: 例如:

phpprepend.php file phpprepend.php文件

<?php
ini_set('session.cookie_domain', '.foo.com');
?>

include the following line in your php.ini file: 在php.ini文件中包含以下行:

auto_prepend_file = "/path/to/file/phpprepend.php"

Please let us know if this solves the problem. 如果这样可以解决问题,请告诉我们。

Good luck! 祝好运!

Use session_name function let they have same session name. 使用session_name函数让它们具有相同的会话名称。 This will work for sub domains. 这适用于子域。 And if they are complete different domains, and if you are using the cookie to pass session id, this will not work because cookie only work for one domain. 如果它们是完整的不同域,并且如果您使用cookie来传递会话ID,则这将不起作用,因为cookie仅适用于一个域。

You need to set up the session cookie's domain so that it is accessible from both sites. 您需要设置会话cookie的域,以便可以从两个站点访问它。

<?php
ini_set('session.cookie_domain', '.foo.com');

Either add that to both sites's PHP code very early on in loading, or add something like this to your .htaccess file in both sites. 可以在加载时尽早将其添加到两个站点的PHP代码中,或者将这样的内容添加到两个站点中的.htaccess文件中。

php_value session.cookie_domain ".foo.com"

I'm not 100% sure the second options works, but I think it should. 我不是100%肯定第二种选择有效,但我认为应该这样做。

Sessions are associated with actual files in the server which contains the session data. 会话与服务器中包含会话数据的实际文件相关联。 So unless the sites have a way of sharing these temporary session files, then they can't share sessions. 因此,除非站点有共享这些临时会话文件的方法,否则它们无法共享会话。 That is unless the session is serialized and sent the other site. 这是除非会话被序列化并发送到另一个站点。 I think the only solution is to implement a session that gets and stores the session data in an external database that the two sites have access to. 我认为唯一的解决方案是实现一个会话,该会话将会话数据存储并存储在两个站点可以访问的外部数据库中。 Then if server 1 sends the session ID to server 2 and server 2 uses it to access the database, it can access the session data stored by server 1 然后,如果server 1将会话ID发送到server 2并且server 2使用它来访问数据库,则它可以访问server 1存储的会话数据

Code igniter has the option of using it's session library which has support for storing your sessions is a database, there may be some form of plugin, or you could build your own, for symphony which can read the code igniter sessions out of the database and setup the session inside symphony. 代码点火器可以选择使用它的会话库 ,它支持存储你的会话是一个数据库,可能有某种形式的插件,或者你可以自己构建,对于symphony可以读取数据库中的代码点火器会话和在symphony中设置会话。

So although they will technically be two different sessions, the database will synchronize them. 因此,虽然它们在技术上是两个不同的会话,但数据库将同步它们。

This of course assumes that your two sites use the same database. 当然,这假设您的两个站点使用相同的数据库。

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

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