I have a private web app with login requirements.
I have seen many sites (mainly forum sites) that list the members that are currently logged in and viewing the site.
I think I can devise a way to look at session vars datetime stamps and make some assumptions, but I was wondering if there was a standard way out there, or a trick that is used, to capture and display that info.
Not looking for code here (per se), just looking for the logic used开发者_如何学Python to do it.
Thanks.
The most common approach is to just update a timestamp every time the user does something, and count her as logged out after a defined inactivity time.
You can use a session variable so you don't have to update the timestamp in the database on every page load. In this variable, keep a timestamp of the last update, and when it's over five minutes ago, update the timestamp in db and session.
You can't know when someone is watching your site, but you can determine when a user last requested a page from your server. If a user has requested a page in (for example) the last five minutes, you could say he's still active and probably looking at a page of your site. As far as I know that's the most common way to determine the number of active users.
How much granularity are you looking for? Do you only want to show users who recently logged into the site, or do you want to display users who are viewing (or recently viewed) a particular forum post?
If the former, it depends on what your login and authentication framework currently looks like, but basically it would be a query of non-expired sessions.
If the latter, you would need to store what users viewed a particular page when. You could use a join table such as Users_Viewed_Pages with a timestamp. Then for a particular page, query the table by Page_ID and display the users who loaded that page recently.
I'd suggest a binary value in your database and toggle it when the user signs on. Then you can have a JS timeout to automatically sign the user out if they're idle for too long.
Implement your own session handler and store active sessions in the database. This also makes it easy to terminate a user's active session and/or keep a log of all sessions.
精彩评论