简体   繁体   English

如何在两个不同的域中共享PHP会话

[英]How to share PHP session in two different domains

Well, here it goes. 好吧,到了。

My client recently buy an SSL Certificate for the domain1.net. 我的客户最近为domain1.net购买了SSL证书。 Weeks later, he bought the domain1.com, which he thinks is better. 几周后,他买了domain1.com,他认为这样更好。

Now, he wants to use the domain1.com but I can't move the certificate from domain1.net to domain1.com because the time for this has expired. 现在,他想使用domain1.com,但是我无法将证书从domain1.net移到domain1.com,因为此时间已到期。

So, the client navigate in domain1.com, and when he needs to login or put credit card information, I redirect to https://domain1.net . 因此,客户端在domain1.com中导航,当他需要登录或输入信用卡信息时,我重定向到https://domain1.net But I need to share the PHP $_SESSION between this 2 domains. 但是我需要在这两个域之间共享PHP $ _SESSION。

How can I do this? 我怎样才能做到这一点? Any ideas? 有任何想法吗? Thank's! 谢谢!

您可以将会话数据放入缓存,这两个域都应该可以访问该缓存(不确定幕后有多少服务器),然后在重定向发生后从缓存中检索会话数据。

You are pretty much out of luck with using $_SESSION functionality I would think. 我认为使用$ _SESSION功能会使您很不走运。 The problem being that you will not be able to pass the session id cookie between the two different domains (you could pass between two subdomains by controlling the cookies domain). 问题在于您将无法在两个不同的域之间传递会话ID cookie(可以通过控制cookie域在两个子域之间传递)。 You could try passing the session id in the query string, but who wants to do that. 您可以尝试在查询字符串中传递会话ID,但是谁愿意这样做。

You could implement you own backend logic to store information common to the two domains, but I would think this would require your own token system to authenticate users to your session id replacement when moving back and forth between domains. 您可以实现自己的后端逻辑来存储两个域共有的信息,但是我认为这需要您自己的令牌系统来在域之间来回移动时对用户进行会话ID替换进行身份验证。

Your best option is to tell your client he needs to buy a cert on the .com domain. 最好的选择是告诉客户他需要在.com域上购买证书。 Either that or he can pay you a significantly greater amount to build you own customized pseudo-session system. 要么,要么他可以付给您更多的钱,以构建您自己的自定义伪会话系统。

You cannot do that, (thank God). 你不能那样做,(感谢上帝)。

You have to find a other way to exchange data: 您必须找到另一种交换数据的方式:

  • Perhaps the 2 sites are on the same server, and can have access to a common directory. 也许这两个站点位于同一服务器上,并且可以访问公共目录。 /tmp for example, thats where the default session files are also. /tmp ,例如,也就是默认会话文件所在的位置。
  • If not on the same server, use ftp, http or another protocol to exchange the data. 如果不在同一服务器上,请使用ftp,http或其他协议交换数据。

Of course, the most easy solution is to buy a new certificate (for a few dollars a year its yours) and merge the sites together. 当然,最简单的解决方案是购买新证书(每年花费几美元,然后将站点合并在一起)。

There is a long way here 这里有很长的路要走

Create a php file(share.php) as following in both domains, data is shared by this method 在两个域中按以下方式创建一个php文件(share.php),数据通过此方法共享

<?php session_start();
echo json_encode($_SESSION);
?>

Now create another file, better to use as include, on another domain for receiving session data 现在,在另一个域上创建另一个文件(最好用作包含文件)以接收会话数据

<?php session_start();
$abc = file_get_contents('http://domain1.com/share.php');
$obj = json_decode($abc);

print_r($obj);

?>

print_r will show you whole session data of another server in this way. print_r将以这种方式向您显示另一台服务器的整个会话数据。 Use combination of objects and arrays to access all elements. 使用对象和数组的组合来访问所有元素。 If you need to overwrite this server's session use this code instead of print_r 如果您需要覆盖此服务器的会话,请使用此代码而不是print_r

<?php

foreach($obj as $key => $value){
$_SESSION[$key] = $value;
}

?>

Do you need the session data or just some login information? 您需要会话数据还是仅一些登录信息? In either case, you can serialize $_SESSION (http://php.net/manual/en/function.serialize.php) or store it however you want in a database. 在这两种情况下,都可以序列化$ _SESSION(http://php.net/manual/zh/function.serialize.php)或将其存储在数据库中。 Add a primary key and a "secret" field which you can fill with rand(0,1000000000); 添加一个主键和一个“秘密”字段,您可以用rand(0,1000000000)填充该字段;

Then when you link to the website, pass the primary key and the secret number and use this to fetch the session information from the database. 然后,当您链接到该网站时,传递主键和密码,然后使用该密码从数据库中获取会话信息。

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

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