跨多个域共享php会话

跨多个域共享php会话

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

问题描述

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



感谢您的帮助..

解决方案

div>

在原始问题的意见中,考虑到您的原始问题和您对Logan和我的回答我理解:



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



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



我相信你有两个选择:



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

  session.cookie_domain =。foo.com

尝试包括它在.htaccess它不会工作,如果你正在运行php作为一个CGI模块,这似乎是在共享托管服务相当常见。



2 - 你可以将文件预置到您网站中的所有php脚本。这些将被放在您的网站运行的每个单个PHP脚本的顶部,甚至CI和Symfony中的。例如:



phpprepend.php档案

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

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

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

请告诉我们是否解决了这个问题。



祝你好运!


I have 2 different domain, let's call it www.foo.com and bar.foo.com. The first one is builded with CI, and the second one is builded with Symfony. I want to share my session, so if I have 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?

thanks for your help..

解决方案

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

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

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

I believe you have two options:

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

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.

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 file

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

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.

Good luck!

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

07-23 13:36