区分 PHP 中的两个会话 [英] Distinguish between two sessions in PHP

查看:42
本文介绍了区分 PHP 中的两个会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过以下方式为本地主机上的网站之一编码.说 localhost/abc:

I tried coding in the following way for one of the website over a localhost. say localhost/abc:

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

输出很好.但是当相同的代码用于同一本地主机上的另一个网页时,请说 localhost/xyz.然后名称之间存在歧义.好像我需要区分 xyz 和 abc 的会话.

The output was good. but when the same code was used for another webpage over the same localhost say localhost/xyz. Then there was ambiguity between names. As if I need to distinguish between the sessions for xyz and abc.

所以我尝试了这个:

<?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.?

推荐答案

简单来说……当您使用相同的浏览器实例访问同一 Web 服务器上的两个不同站点时,您正在访问服务器的相同内存区域.于是

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. Thus

http://localhost/xyzhttp://localhost/abc 指的是同一个站点 localhost,因此您不会通过 session_start() 而是恢复它.您也可以像 Jon 所说的那样创建虚拟主机,但为了测试我猜您是这样,只需使用不同的浏览器.

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. You can alternatively create virtual hosts as Jon said but for the sake of testing which I guess you are, just use different browsers.

另外,你不能正常地在不同的机器上共享会话,所以我认为这是你的逻辑错误.或者尝试

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.

这篇关于区分 PHP 中的两个会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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