Tracking registered users in PHP

I am trying to write a simple web chat application with PHP and AJAX.

I need to know about all open sessions so that I can display a list of online users I can talk to. I also need to know about logging outs because I use “both sending and receiving side offline” as a condition for considering a chat session that has completed and deletes messages.

I track the user login in the database: adding an entry at the system and deleting it at the system logout works fine, but it is not complete, because there are two other ways that the user can log off

  • A server session expires after no activity.
  • client-side cookies are destroyed when the browser is closed. It seems like a bad idea to use some kind of activated AJAX (what if the browser crashes or something?).

The simplest solution seems to keep the timestamp of the last activity. I see some problems with this:

  • The AFAIK server-side outcome is based on chances, so it will not be accurate (and if I get the expiration time 3 minutes incorrectly, then 3 minutes when some guy can talk to the user offline, wondering why no one is responding)
  • I would have to constantly query the database in order to check each last recorded time of user activity compared to the current time. I do not see when / where I will do it effectively. It seems silly to do this every time you need a list of online users.

Any help appreciated. I myself encode this because I don’t know of any web chat frameworks that can integrate with my existing user database, correct me if I am wrong.

+6
source share
7 answers

I don’t think that you can do much to reduce the constant request, to determine if users have logged out by closing the browser, problems with the Internet connection, etc., but you may have an AJAX request to the server every 5 seconds to update the latest activity time, and ask the application on the server to consider that the user has "logged out" if they missed 3-4 consecutive requests (i.e. their last activity time is> 20 seconds).

On the client side, you can check the latest activity time each time your client sends a message to another user, and reply that they are logged out if this happened. If they try to open a chat with another user, you can also make an immediate call to check their status. Then you can check the status of all users in the user list every 30 seconds. Thus, your client receives a fairly quick feedback if the person (s) with whom he is talking unexpectedly disconnects.

+1
source

You can invert your template by replacing the behavior of Ajax pull with a push notification system.

Thus, you can notify your chat users in real time of logging in and logging out of new chat members. I have never done anything like this in practice, but I read about this technology and it seems to be very interesting for cases like yours.

This is a bit more complicated than the ajax pull path, but once the basic structure is implemented, you can easily add functionality and performance will be much better.

Some links I found may be helpful:

This is a javascript chat episode of railscast, the implementation runs on rails, but even if you don't understand the rails, you have to follow it to get the basic concepts: http://railscasts.com/episodes/260-messaging-with-faye

+1
source

I'm not sure if you need both probability and a divisor, but here is what I do to automatically log out:

ini_set('session.gc_maxlifetime',3600); // 1 hour ini_set('session.gc_probability',1); // it does garbage cleaning EVERY time ini_set('session.gc_divisor',1); // it does garbage cleaning EVERY time session_start(); 
0
source

Most of the data you are currently using and the data you need are stored in a PHP session - it’s just not clear how to get the user ID from the information.

If you switch to using a database-bound session handler, things get really easy. I had a quick google - and there are many examples. but many of them (like this one ) do not check for expiration when reading a session. The OTOH in the example I linked to shows adding a user ID to a session record — which you'll need later. Thus, the function of the read session handler should look something like this:

  read: SELECT session_data, username FROM sessions WHERE session_id=' . session_id() . ' AND last_updated>= ' . date('YmdHis', time()-ini_get('session.gc_maxlifetime')) 

And to get all currently registered users:

  SELECT username FROM sessions WHERE last_updated>= ' . date('YmdHis', time()-ini_get('session.gc_maxlifetime')) 

... and let the (overridden) session garbage collector automatically clear the redundant data.

NTN

0
source

Separate your concerns first. Regarding the "online user list", you can use the database, and it looks like you already understood that. (even if someone does not log out properly, displaying several additional online users does not do much damage)

Now for the chat application to check if the user is online, you will need to use ajax. There is simply no other way. Of course, there can always be a hack, I don’t know. see image image when you answer here (stackoverflow) .it constantly checks to see if the time has passed (and if something new has been typed) and saves a copy.

0
source

It seems that you are most worried about performance, and you have figured out the implementation details in detail (mostly). Just change the type of table that processes the sessions in memory, this will reduce the cost of executing a db query for each query, almost nothing, since you are retrieving data directly from RAM. Just make sure you delete the session every time a user error is triggered, or you mark the user as inactive.

But no matter what you implement, something that requires constant communication between the client and the server can never be done over HTTP. But if you set reasonable timeouts, etc., It will work in 99% of cases.

0
source

I'm late for this party, but let me give my two cents.

In this particular scenario, there is no need for a push system for bad notifications. Also there is no need for cron. Let me explain:

Say that A, B and C are in the chat, and browser B is crashing, so his session will expire. Right now, the server thinks that B is still there, but she is not. How do you upgrade A and C? When A and C ASK to upgrade. Check the last timestamp B and find out that her session has expired.

If all accidents A, B and C, then this will never happen, I heard you ask. What's the difference? Now there is no one to see our mistake! The only drawback is maintaining a live chat room that is in the database space. This can be cleared by creating another chat session.

Final meaning:

  • Save timestamps the last time an action has been taken.
  • Use a custom event to scroll through timestamps and pull out a dead tree.

In the case of users, this will result in their registration. In the case of chats, these will be the ones that have expired.

0
source

Source: https://habr.com/ru/post/895742/


All Articles