dthl8036
2013-07-29 17:42
浏览 35
已采纳

是否可以从其他会话中访问$ _SESSION变量?

My website uses sessions for user logins and I would like to have a message on each page showing the user how many computers are logged into their account (like what Gmail does).

For example, if they are logged in on two different computers, I would like the message to say:

You are logged in on 2 computers.

I know how to check if they are logged in -- I just check the $_SESSION['username'] variable and if it's set then they are logged in on that computer. But how can I check if the same username is set in other sessions (indicating that they are logged in on other computers)?

图片转代码服务由CSDN问答提供 功能建议

我的网站使用会话进行用户登录,我希望每个页面都显示一条消息,向用户显示有多少台计算机 登录到他们的帐户(就像Gmail一样)。

例如,如果他们在两台不同的计算机上登录,我希望该消息说:

 您已登录 在2台计算机上。
   
 
 

我知道如何检查他们是否已登录 - 我只需检查 $ _ SESSION ['username'] 变量,如果已设置,则他们将登录该计算机。 但是如何检查在其他会话中是否设置了相同的 username (表明他们是否已在其他计算机上登录)?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • douweihui0178 2013-07-29 17:46
    已采纳

    This is very difficult to do with the default file based PHP sessions. By default PHP just stores session information in an opaque blob in a file in the file system, with the name of the file being the session id. You have no direct access to the data in those files without opening them all one by one and evaluating them.

    What you need is to move that information into a database. Either store only the number of concurrent logins in the database and keep the other sessions as is, but that's hard to keep in sync. It's better to switch to a complete database backend for sessions in lieu of the file based storage. This allows you to transparently query any data in sessions, including easily taking statistical information about how many sessions a user has.

    评论
    解决 无用
    打赏 举报
  • 查看更多回答(3条)

相关推荐 更多相似问题