开发者

Questions about making 'if user is online' system

开发者 https://www.devze.com 2023-04-01 15:41 出处:网络
Please help start in the right way . I want to add \'if user is online\' system. Which I will use to prevent multiple logins under 1 name, also this system will add option to users showing them who is

Please help start in the right way . I want to add 'if user is online' system. Which I will use to prevent multiple logins under 1 name, also this system will add option to users showing them who is online at the moment.

I understand that I need mysql table where I will store online u开发者_开发问答sers. But how can I understand each second if user is still here? if he logs out then ok, it's easy, but if he will restart? or something else ? So how can I controll all users and understand per second their status ?

UPDATE After some discussions with Cupcake I decided to have only who's online feature, letting 1 user to log in multiple times, cause it's difficult to prevent him from doing that in a comfortable way.


renew this table record each time user requests a page, updating access time field.
delete from this table all records which access time field is older than some reasonable timeout like 5 min

to prevent multiple logins under 1 name you have to store session id in the users table. when user logins, stire current session id in this field every time user requests a page, compare this id against actual session id, and make user relogin


What you could do, which is what I sometimes do, is this, in the user table have a column named session_hash or something similar.

And each time the user logs in, generate a new session hash yourself or use the session_id PHP has, as long as you use session_start on each page.

Then to retrieve the users row from the database, have a cookie with that session_hash.


Example

Login form

Username:  [       ]
Password:  [       ]
Remember?: [*]
           [ Login ]

PHP page does the following

User logs in ->
  System generates a session_id or "salt"
  Updates the users row with the session_id or "salt" value

  Check if the value of the "remember" checkbox is true ->
    Sets a cookie with that session_id or "salt" with a month or so expiration date
  Otherwise just create a normal cookie with the value

Then on each page do the following

Check if the session_id or "salt" cookie is set ->
  If it is, fetch the users row from the database
    SELECT * FROM users WHERE session_id/"salt" = value of cookie

What all this does if lets the user choose if they want to be remembered each visit, if they do create a cookie that doesn't expire for at least a month that way they will be remembered next time they come to the site, but if they login from another browser or computer the "salt" won't be valid on the other computer.


Simply invalidate all old sessions of the user, once he opens a new one. In that way, he can only run one session (aka one login) at a time.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号