简体   繁体   English

在PHP中区分两个会话

[英]Distinguish between two sessions in PHP

I tried coding in the following way for one of the website over a localhost. 我试着通过以下方式为本地主机上的一个网站编码。 say localhost/abc: 说localhost / abc:

<?php
session_start();
$_SESSION['name']=$row['name']
?>

The output was good. 产量很好。 but when the same code was used for another webpage over the same localhost say localhost/xyz. 但是当同一个代码用于同一个本地主机上的另一个网页时,请说localhost / xyz。 Then there was ambiguity between names. 然后名字之间存在歧义。 As if I need to distinguish between the sessions for xyz and abc. 好像我需要区分xyz和abc的会话。

So I tried this: 所以我尝试了这个:

<?php
session_id(226);
session_name(veer);
session_start();
.
.
.//connection with database and all
.
$_SESSION['name']=$row['name'];
echo($_SESSION['name']);
?>

When another machine logged in over the same server then the session I created was accessible by that machine for same webpage. 当另一台计算机通过同一服务器登录时,我创建的会话可由该计算机访问同一网页。

Is there any solution. 有什么解决方案吗? Or how to distinguish between two sessions.? 或者如何区分两个会话。

To put it in simple terms... you are accessing same memory area of server when you access two different sites on same web server using the same browser instance. 换句话说,当您使用相同的浏览器实例访问同一Web服务器上的两个不同站点时,您正在访问服务器的相同内存区域。 Thus 从而

http://localhost/xyz and http://localhost/abc are referring to the same site localhost and thus you will not start another session by session_start() but instead resume it. http://localhost/xyzhttp://localhost/abc指的是同一站点localhost,因此你不会通过session_start()启动另一个会话,而是恢复它。 You can alternatively create virtual hosts as Jon said but for the sake of testing which I guess you are, just use different browsers. 你也可以像Jon说的那样创建虚拟主机,但是为了测试我猜你是哪个,只需使用不同的浏览器。

Also, you cannot share session over different machines normally, so I think that's your logical mistake. 此外,您无法正常在不同的计算机上共享会话,因此我认为这是您的逻辑错误。 Alternatively try 或者尝试

session_start();
echo (session_id());

on the top of the page and see if you are starting or resuming the same session which I think you are not. 在页面顶部,看看你是否正在开始或恢复我认为你不是的同一个会话。 I think your page is storing same data in different sessions which you are mistaken as same session. 我认为你的页面在不同的会话中存储相同的数据,你被误认为是同一个会话。

Use session_regenerate_id(); 使用session_regenerate_id(); method in the second file(xyz). 第二个文件中的方法(xyz)。

this? 这个?

<?php
session_start();
if (!isset($_SESSION['xyz'])) { $_SESSION['xyz'] = array(); }
$_SESSION['xyz']['name'] = $row['name'];
?>

sometimes instead of doing the above i just prefix my session keys example: $_SESSION['xyz_name']; 有时而不是做上面的我只是前缀我的会话键示例:$ _SESSION ['xyz_name']; I did that after i realized that my CPanel has been used some sessions of its own that caused a conflict to mine. 在我意识到我的CPanel已经使用了自己的一些会话导致我的冲突后,我就这样做了。

Requests from the same user agent to the same web server will share the same session, barring explicit configuration that depends on your exact server setup. 来自同一用户代理到同一Web服务器的请求将共享同一会话,禁止显式配置,具体取决于您的确切服务器设置。

Normally this problem is avoided because the "other webpage" would actually be on another domain entirely, so the session cookie (and by extension the session data) would not be shared. 通常这个问题是可以避免的,因为“其他网页”实际上完全在另一个域上,因此会话cookie(以及扩展会话数据)将不会被共享。 This is also what you should do if you want to run separate applications independently on localhost : set up separate virtual hosts on separate internal domains. 如果要在localhost独立运行单独的应用程序,也应该执行此操作:在单独的内部域上设置单独的虚拟主机。

You could also solve the problem purely in code by not using $_SESSION directly to store your data but a subkey based on some differentiating factor such as $_SESSION['SCRIPT_NAME'] . 您也可以通过不直接使用$_SESSION来存储数据,而是使用基于某些差异因素(如$_SESSION['SCRIPT_NAME']的子键来完全解决问题。 A very simple example: 一个非常简单的例子:

$sessionKey = "default";
if (strpos($_SESSION['SCRIPT_NAME'], "dir1/")) {
    $sessionKey = "dir1";
}
else if (strpos($_SESSION['SCRIPT_NAME'], "dir2/")) {
    $sessionKey = "dir2";
}

$_SESSION[$sessionKey]['mydata'] = 'foo';

However this last one is a really inelegant solution which I would not recommend. 然而,最后一个是一个非常不优雅的解决方案,我不建议。

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

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