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

  • 写回答

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条)

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题