简体   繁体   中英

How to know if user is logged with his session id

I need to know if there is an user logged in my website. For this purpose, I have only his session ID.

I got this id using: session->getId();

¿Is it possible?

Thanks in advance.

在用户登录时将自定义字段添加到会话中,然后仅检查该字段

Given that the web is essentially stateless, it is hard to know for sure if a particular user is logged onto a website.

One way to accomplish your goal is to keep a running log of all the users logged in, and the last visit time. Then you could query that log and if the users last visit time was less than 5 minutes ago, you could then say the user is logged onto your site. This will only tell you if the user is logged into your site, not if your site is the active tab in their browser.

Another way to get more "real-time" information as to the the active users of your site is to use something like SignalR which will allow you to do push to the browser. I've used SignalR in the past to send out system status messages to the browser and have it automatically update the page. The great thing about SignalR is it maintains an in-memory list of all the clients connected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM