Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a user is online

Tags:

php

I'm trying, to check if a user is online on my website, for example the user is logging in into the site, a php code would update the db row called online to '1', incase he logs out to '0'.

But if the user just exits the site, the row won't be updated, how could I check if the user is actually online.

The login is also creating a session, which contains the following:

$_SESSION['logged_in'] = true;

How could I check this?

like image 365
user2452165 Avatar asked Sep 30 '16 17:09

user2452165


People also ask

How do you check whether user is online or not?

We can detect if the user is online or offline by using the online property on the navigator object which will return true if the user is connected to the internet, otherwise it returns false . Additionally, online and offline events will be triggered if the user's internet connections state changes.

How check user is online or not in PHP?

php page does three jobs. First it update the status of the logged in user with new time and set status to ON. Here is the code of this first step. $tm=date("Y-m-d H:i:s"); $q=mysql_query("update plus_login set status='ON',tm='$tm' where id='$session[id]'");


2 Answers

You can't really do it this way. You could update a last_activity column on every pageview and consider any user who hasn't had a pageview in 10-15 minutes to be inactive, but there's no reliable way to know exactly when the user left the site just with PHP.

If this is a strict requirement (like for a chat application, perhaps) you'll need to investigate real-time communication using websockets.

like image 107
ceejayoz Avatar answered Oct 19 '22 07:10

ceejayoz


You can use publish-subscribe pattern. Let’s understand what is publish-subscribe pattern.

publish–subscribe is a messaging pattern where senders of messages, called publishers, do not program the messages to be sent directly to specific receivers, called subscribers, but instead characterize published messages into classes without knowledge of which subscribers, if any, there may be. Similarly, subscribers express interest in one or more classes and only receive messages that are of interest, without knowledge of which publishers, if any, there are.

Source: Wikipedia

Below is an example using RabbitMQ MQTT Adapter:

Subscribe user A’s app to a topic “/topic/user-a”, user B’s app to a topic “/topic/user-b” and publish the online/offline status to a topic "/topic/presence". Create a program on your backend server to subscribe to the "/topic/presence". If any update comes from lets say User A then publish the update to all friends to User A. This way, User B will receive the online/offline update of User A.

                User A             User B            PresenceListener

Subscribe       /topic/user-a      /topic/presence   /topic/presence

Publish         /topic/user-b      /topic/presence   friend list

Real challenge here is how to publish "offline". One case is if user closes the app while internet is still active then the app can publish the "offline" status to the server but what happens when the internet stops working?

Lets go through "last will and testament" (lwt).

LWT messages are not really concerned about detecting whether a client has gone offline or not (that task is handled by keepAlive messages). LWT messages are about what happens after the client has gone offline. LWT messages can be leveraged to define a message to be published by the broker on behalf of the client, since the client is offline and cannot publish anymore.

Source: http://tuanpm.net/what-is-mqtt/

For a sample source code with online offline presence, you can checkout our Applozic Chat Javascript Plugin code available in Github https://github.com/Applozic/Applozic-Web-Plugin/

Demo page: https://www.applozic.com/docs/chat-examples/web.html

like image 21
Devashish Mamgain Avatar answered Oct 19 '22 07:10

Devashish Mamgain