dthl8036 2013-07-29 09: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 09: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部