Whats the easiest way to determine if a user is online? (PHP/MYSQL)

前端 未结 10 1245
轻奢々
轻奢々 2020-12-02 05:24

Is there a way I can piggy back sessions to know if the user is online?

I.e: use logs on, I set a $_SESSION variable, user times out- cookie Garbage collector update

10条回答
  •  心在旅途
    2020-12-02 06:13

    The solution that I have implemented for this is to, on every page load by an authenticated user, set/reset a memcache var such as "user_{userid}isonline" => true and expire it in 5 minutes. Then check if the var is in the cache when I access the user's info. Depending on the size of your user base, if you want to get a list of everyone online, you could use a memcache getmulti call with an array of "user{userid}_isonline" keys for all of your users.

    of course, this really depends on how often a user will change pages on your site... to get a more accurate representation of the users online, you could implement an ajax xmlhttprequest call on your page running at a small interval (30 seconds or so) that resets the memcache var, and have the memcache var expire in less time (1 minute to account for possible browser issues). This is not COMPLETELY accurate, but as http does not have a persistent connection to the server, you are pretty limited on what you can do.

    IF you require an up to the second representation of who is online, you could maybe have a flash app loaded in your page that connects to a jabber server, then just check if that particular user is logged in on the server.

提交回复
热议问题