跨多个域共享 php 会话($_SESSION) [英] Sharing php Session ($_SESSION) across multiple domain

查看:33
本文介绍了跨多个域共享 php 会话($_SESSION)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个不同的域,我们称它们为 www.foo.com 和 bar.foo.com.第一个是用 CI 构建的,第二个是用 Symfony 构建的.我想分享我的会话,所以如果我登录其中一个,我可以访问另一个.我使用 $_SESSION["session_name"] = "value"; 设置我的会话数据.

I have 2 different domain, let's call them www.foo.com and bar.foo.com. The first one is built with CI, and the second one is built with 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";.

如何使会话数据可以从其他域读取?

How to make a session data readable from the other domain?

感谢您的帮助.

推荐答案

考虑到您的原始问题以及您在原始问题的评论中对 Logan 和我自己的回答,我理解:

Considering your original question and your answers to Logan and myself in the comments of the original question I understand:

1 - 您想在域及其子域之间传递会话变量;和

1 - you want to pass the session variables among a domain and its subdomains; and

2 - CI 和 Symfony 在你有机会执行 ini_set 命令之前加载会话.

2 - CI and Symfony load the session before you have a chance to do the ini_set command.

我相信你有两个选择:

1 - 在 php.ini 文件中包含 php 配置命令

1 - include the php configuration command in the php.ini file

session.cookie_domain=".foo.com"

如果您尝试将其包含在 .htaccess 中,如果您将 php 作为 CGI 模块运行,则它将不起作用,这在共享托管服务中似乎相当普遍.

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.

2 - 您可以在站点中的所有 php 脚本中添加一个文件.这些将放在您站点运行的每个 php 脚本之上,甚至是 CI 和 Symfony 中的脚本.例如:

2 - you can prepend a file to all php scripts in your site. Those will be put on top of every single php script your site runs, even the ones inside CI and Symfony. For example:

phpprepend.php 文件

phpprepend.php file

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

在您的 php.ini 文件中包含以下行:

include the following line in your php.ini file:

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

如果这能解决问题,请告诉我们.

Please let us know if this solves the problem.

祝你好运!

这篇关于跨多个域共享 php 会话($_SESSION)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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