[英]How can I manage separate session states for two different websites on the same hosting using php
I am currently developing two web sites, and debugging them by connecting to localhost
. 我目前正在开发两个网站,并通过连接到localhost
来调试它们。
The first site is referenced with http://localhost/web1
and the second is referenced with http://localhost/web2
. 第一个站点使用http://localhost/web1
引用,第二个站点使用http://localhost/web2
引用。
I have created a login script for each in which three domain-specific session variables are set, eg: 我为每个创建了一个登录脚本,其中设置了三个特定于域的会话变量,例如:
$_SESSION['web1_user']
$_SESSION['web1_login']
$_SESSION['web1_sessionID']
However, when I log in to both sites on the same browser , then log out of one site (which fires session_destroy()
, I am automatically logged out of the second site as well. 但是,当我在同一浏览器上登录到两个站点时,然后注销一个站点(触发session_destroy()
,我也会自动从第二个站点注销。
Any ideas as to how I might resolve this problem would be very much appreciated. 关于如何解决这个问题的任何想法都将非常感激。 :( :(
Ahhh, the pleasures of shared hosting! 啊,共享主机的乐趣! The best thing to do is simply use a different browser for each site whenever you actually require being logged in to both sites simultaneously... 最好的办法是,只要您实际需要同时登录到两个站点,就可以为每个站点使用不同的浏览器...
To explain why this is important, you must understand the following, however: 要解释为什么这很重要,您必须了解以下内容:
Session
variables are stored on the server, with a keyed reference on the server and a cookie on your browser. Session
变量存储在服务器上,服务器上有键控引用,浏览器上有cookie。 Once you unset and destroy either of the two, a match can no longer be made - and your session is gone! 一旦你取消并摧毁了两者中的任何一个,就不能再进行一场比赛了 - 你的会话就不见了!
session_start();
session_unset();
session_destroy();
The above will kill all session variables linking the server to your browser (on the server side). 以上将删除将服务器链接到浏览器的所有会话变量(在服务器端)。
The way to manage this easily is to make session variables into another set of arrays: 容易管理的方法是将会话变量转换为另一组数组:
$_SESSION["site1"] = array( $user_id, $session_id );
$_SESSION["site2"] = array( $user_id, $session_id );
You could of course make it fancy: 你当然可以把它变成幻想:
$_SESSION['site3']['userID'] = 'someuserid';
$_SESSION['site3']['sessionid'] = 'somesessionid';
Then when you logout from site 1 然后当您从站点1注销时
session_start();
unset($_SESSION['site1']);
In this case, you have created a separate session management system for each site (using a two-dimensional array, the top layer of which is keyed by your site's identifier). 在这种情况下,您为每个站点创建了一个单独的会话管理系统(使用二维数组,其顶层由您站点的标识符键控)。 This makes it so that each site manages a separate set of session variables - and when you destroy one, you do not touch the others. 这使得每个站点管理一组单独的会话变量 - 当你销毁一个会话变量时,你不会触及其他变量。
However, I realllllllllly recommend using different browsers instead (or in addition)... 但是,我将建议改为使用不同的浏览器(或者另外)...
I recently solved a problem which is related to your question. 我最近解决了与你的问题有关的问题。 Originally, I was looking for an implementation similar to what you are describing, and after doing quite a bit of searching around - this is what I came up with: 最初,我正在寻找类似于你所描述的实现,并且经过相当多的搜索 - 这就是我想出的:
Site 1 : 网站1 :
ini_set("session.cookie_domain", "yourdomainname");
$some_name = session_name("some_name");
$domain = 'your domain name';
session_set_cookie_params(0, "/", $domain);
session_start();
$_SESSION['user']=$_POST['user'];
$_SESSION['password']=$_POST['password'];
Site 2 : 网站2 :
$some_name = session_name("some_name");
ini_set('session.cookie_domain', 'yourdomainname');
session_start();
echo $_SESSION['user'];
echo $_SESSION['password'];
This change worked well for me - and my guess is that it will also help you. 这种改变对我很有用 - 我的猜测是它也会对你有所帮助。
Use 使用
session_name('web1');
before session_start();
在session_start();
之前session_start();
通过session_name()
或session.name
在每个应用程序中设置不同的会话名称。
你可以用它
ini_set("session.cookie_domain", ".example.com");
You need to make a different host for different site 您需要为不同的站点创建不同的主机
in this case you have two site running on same host called localhost so for same host name sessions are shared. 在这种情况下,您有两个站点在同一主机上运行,名为localhost,因此共享相同的主机名会话。
Include the file with the session start in the second domain. 在第二个域中包含会话开始的文件。
web1 contains the session start file, web2 include('../web1/session.php'); web1包含会话开始文件,web2包含('../ web1 / session.php');
You can use different session name in all website like for first website you have used $_SESSION['web1_user'], $_SESSION['web1_login'], $_SESSION['web1_sessionID']
then second website you can use $_SESSION['web2_user']
您可以在所有网站中使用不同的会话名称,例如您使用的第一个网站$_SESSION['web1_user'], $_SESSION['web1_login'], $_SESSION['web1_sessionID']
然后第二个网站您可以使用$_SESSION['web2_user']
I have already face this problem and solved it using different name of session. 我已经面临这个问题并使用不同的会话名称解决了它。
Bez sessions are shared in the same browser, so if you logout from one tab, the other tabs will be logged out, Bez会话在同一浏览器中共享,因此如果您从一个选项卡注销,其他选项卡将被注销,
Example: I login in Chrome, and I open in another Chrome, the Sessions are shared, so if i logout from one, the other one gets logged out automatically! 示例:我在Chrome中登录,并在另一个Chrome中打开,会话已共享,因此如果我从一个登出,则另一个会自动注销!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.