How to know who are online at present in any system
We can find out who are online or logged in to the system. This also displayed as members active now at the footer of any site or in any login system. This is integrated in any login system of any site. We will try to learn how such a system works and display the names ( or user ids ) of the members active at the site. This is part of any system where members login with their user id and password.
We will move with some steps and to learn this, before that please read our member login, signup system with profile update etc. We will add the who is online system to this login script so a better understanding of member signup script.
As you can understand the member session details after login are stored at server side and not at the client end. So getting the details from the server is not possible as scripts will not have root access. So we will try to maintain one more parallel system to know who are logged in. Here some steps we have taken exclusively for this and let us learn different steps of this script.
Storing the login information in a table
Every time a member successfully loges in we will store the details by adding a record to our table exclusively created for this purpose. We call this table as plus_login. It has five fields storing session id inside field name id, usrid storing user id of the logged in member, ip address of the system from where member has logged in, time of logging in the field name tm and status which will be setting to OFF once the member logged out and we will set it to ON by default. So once a member logged in we will store all these information by inserting a record where the status by default gets set to ON. The structure of the table ( sql dump ) is available inside the download zip file at the end of this tutorial.
Updating the status of the member
We have the status stored in our plus_login table where we have set the status to ON. Along with the status we are also storing the time of logged in. Now we have to update this status to ON and update the new time (field name tm) on every time the member opens inside the member area. We have kept the code to update the plus_table with new time and status to ON inside the page bottom.php. This page bottom.php is called by all pages at the end. This bottom.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.
Second it mark the status to OFF for all the members who has not interacted with the site within last 10 minutes. ( that is the reason we will be making the status ON and new time for every page call of the member ). Here is the code inside bottom.php for this step.
$q=mysql_query("update plus_login set status='ON',tm='$tm' where id='$session[id]'");
Here is the query to do this
$gap=10; // Gap value can be changed, this is in minutes.
// let us find out the time before 10 minutes of present time. //
$tm=date ("Y-m-d H:i:s", mktime (date("H"),date("i")-$gap,date("s"),date("m"),date("d"),date("Y")));
At third step it collects the member login ids for which the time is within last 10 minutes and status is set to ON. They are our logged in members or who are active at site. The code you can see one Displaying who are active at site section ( scroll down )
$ut=mysql_query("update plus_login set status='OFF' where tm < '$tm'");
What happens when a user logs out
We change the system status to OFF and destroy the session. We have kept this code inside logout page.
$q=mysql_query("update plus_login set status='OFF' where id='$session[id]'");
What happens when member does not interact for more than 10 minutes?
You can see every time any page is opened and if the page is calling bottom.php page then all users will be changed to status OFF if their tm ( field ) is less than 10 minute of present time. So if no other member has opened any page then the status will not change to OFF. If you think this is to be taken care then you can use one scheduler ( win server ) or cron to run the status update part once in every 10 minutes. Check the query above at the third step.
Displaying who are active at site
We have kept the code inside bottom.php file, so from any other page this page can be called. You can see the code inside this page where we have used one simple query to collect the user id for which status is ON and time field ( tm ) is more than present time minus 10 minutes. The query is here .
Rest is simple display of user ids , you can see how to display in our data display tutorial.
$qt=mysql_query("select userid from plus_login where tm > '$tm' and status='ON'");
That's all, download the zip file with who is online code here Use the forum to post your feedbacks and bugs if any.