How to know who are online at present in any systemWe 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 tableEvery 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 memberWe 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.