如何在不同的电脑上访问php会话
我正在做一些关于在 php 中使用会话的练习。我是这样设置的:
$_SESSION['log_users'][] = array(array('Username'=>$username))
我尝试对其进行实验。并发现当我使用不同的 IP 访问它时,生成的会话是不同的。同时使用相同的浏览器,即firefox。
我所做的如下:
- 设置我的路由器,以便其他人能够通过使用我的外部 IP 地址访问我正在进行的练习。
然后我打开了练习的本地主机版本:
然后使用外部IP地址:
然后我在每个浏览器选项卡上填充会话数组。并发现这两个中的每一个都保留了不同版本的会话。通过使用
发现,print_r($_SESSION['log_users'])
这真的是它应该表现的方式吗?我可以做些什么以使会话只有一个版本吗?我目前使用的是 Wampserver 2.1
I'm doing some exercises on using sessions in php. I set it up this way:
$_SESSION['log_users'][] = array(array('Username'=>$username))
I tried to experiment on it. And found out that the session that is being generated is different when I use a different ip for accessing it. While using the same browser, which is firefox.
Here is what I did:
- Setup my router so that others will be able to access the exercise that I'm working on through the use of my external ip address.
I then opened the localhost version of the exercise:
Then the one using the external ip address:
I then filled up the session array on each browser tab. And found out that each of those two keeps a different version of the session. Found out by using
print_r($_SESSION['log_users'])
Is this really the way it should behave? Is there anything I can do so that there's only one version of the session? I'm currently using Wampserver 2.1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
会话存储在服务器端,并在客户端创建会话cookie,以识别保存当前会话id的浏览器的当前会话。
会话 cookie 是根据您用于访问站点的域来存储的。
由于您使用不同的域,一个是 localhost ,另一个是 ip ,这将创建两个不同的会话。
当您通过
localhost
域访问页面时。它将创建会话并在域localhost
上存储会话 cookie。如果您访问同一域系统上的另一个页面,将检查会话 cookie 是否存在,它会恢复旧会话并且不会创建新会话。同时,如果您通过
ip
访问,则不会为该ip
存储会话 cookie,但系统会假设该用户没有活动会话,并将启动一个新会话会话和会话 cookie 是基于此ip
存储的。这就是会话的工作方式。
希望这有帮助。
The session is stored on server side and a session cookie is created on client side to identify the current session of browser which holds current session id.
The session cookie is stored based on the domain you are using to access the site.
Since you are using different domain one is
localhost
and another isip
which will create two different sessions.When you visit pages through
localhost
domain. It will create session and store session cookie on the domainlocalhost
. If you visit another page on same domain system will check if the session cookie exists it resume the old session and does not create new one.While the same time if you access through
ip
the session cookie is not stored for thisip
yet then system assume that there is no active session for this user and will start a new session and session cookie is stored for based on thisip
.This is the way how session works.
Hope this helps.
会话cookie与域名绑定。当您第一次访问它时,它将绑定到
localhost
域。如果您随后将浏览器指向 201.xx.xx.xx 地址,则该域名将不再匹配。并且您的浏览器将不会再次发送此 cookie。这就是为什么会生成新会话的原因。即使它实际上是同一台服务器。
The session cookie is bound to a domain name. When you first access it, it will be bound to the
localhost
domain.If you then point your browser to the 201.xx.xx.xx address, the domain name will no longer match. And your browser will not send this cookie again. This is why a new session will be generated. Even though it is factually the same server.
在你的情况下,只有 $_SESSION 对你没有帮助。您还应该尝试使用 $_SESSION 和数据库。
您应该同步您的会话和数据库会话记录。
系统将检查您在数据库中的条目。如果您有可用的条目,那么它将直接为您的站点生成会话。这样,所有浏览器只能使用一次登录。
In your case only $_SESSION will not be helpful for you. You should try with $_SESSION and Database also.
You should synchronize your session and database session record.
System will check your entry in database. If you have entry available then it will directly generate session for your site. This way only one login can be available for all browsers.
虽然 Shakti Singh 的答案在技术上可能是正确的。
在我看来,你似乎试图实现的目标是无法实现的。
使用 session_id() 加数据库的方式可能大致如下:
While the answer from Shakti Singh may be technically correct.
The goal you seem to be trying to achieve is imo not reachable that way.
The way with session_id() plus database may look roughly like: