PHP and Ajax Online User for Admin System

Admin Panel or Back-end site is used to manage and monitor the client or front-end side of the web system. Online user system is very useful to monitor the users to know who is being online, what page they are in, how long have they been online, and so on. With this function, the administrators will be able to monitor the activities of their users by just check the use online status. This feature is very important and necessary for advanced web system like Online Trading System, Online Market System, Online Game System, and other online systems. This feature is generally used with Risk Management System of the CRM of the advanced online web system to monitor the users’ activities.

INSTRUCTION

MySQL Database

CREATE TABLE IF NOT EXISTS `tblonlineusers` (
  `userid` int(11) NOT NULL,
  `activePage` text NOT NULL,
  `timestamp` varchar(20) NOT NULL,
  `startOnlineTime` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `tblusers` (
  `id` int(11) NOT NULL,
  `name` varchar(200) NOT NULL,
  `email` varchar(100) NOT NULL,
  `active` INT(1)  NOT NULL DEFAULT '1'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

In this tutorial, we need two database tables to store the online information and basic user information.

  • tblonlineusers
  • tblusers

These tables contain only the basic information, so we can add more information according to what we really want. However, let’s take these basic database tables for this tutorial.

Client JS File (c_ajax.js)

var currentURL=window.location.pathname;
$.post("c_getData.php",{ userid:userid,currentURL:currentURL } ,function(data){
//do something to display returned data if needed
});

This Ajax jQuery run only when the user successfully logged into the system. And, this is the very important file that used to send the command request with some data to the server to check the online user information. In the above script, we send two variables userid and currentURL.

  • userid: the logged in user id
  • currentURL: the current active page where use is locating

Normally, there is no data to be returned because this command request is just to run update the online status of the users. However, we can return some information back and display to the website by echo some data in the requested php file.

To obtain real time online user status, we need to do time interval that request using time interval function or setTimeout function.

Admin JS File (a_ajax.js)

$.post("a_getData.php",{} ,function(data){
var data = JSON.parse(data);	
//do something to display returned data
//data returned json data is: data.id, data.name,data.activePage
});

This is the Ajax jQuery file for admin system. This Ajax request is used to get the current user online status for the database. In the above script, we did not send any data to the requested php file because we just want to get the whole user online information. However, if we want to check only few users or user group or a specific user, we need to send the input data to sending data block inside {}.

The above script returns data in the form of Json data (array data) having 3 property names.

  • data.id : return back the userid
  • data.name : return the full user name
  • data.activePage : return the current page of the user

To get all the data for the Json, we can do either each loop or for loop and do something with each element of that Json object to display them to the web.

To obtain real time online user status, we need to do time interval that request using time interval function or setTimeout function.

Client PHP File (c_getData.php)

$dbhost = "localhost";
$dbuser = "khcoder";
$dbpass = "khcoder***";
$dbname = "khcoder";
$conn = mysqli_connect($dbhost,$dbuser,$dbpass,$dbname) or die("Error " . mysqli_error($conn));

$timestamp=time();
$inactiveTime = 10;
exec_query_utf8("DELETE FROM tblonlineusers WHERE ($timestamp-timestamp) > $inactiveTime");
	
$totalOnline=mysqli_num_rows(exec_query_utf8("SELECT * FROM tblonlineusers WHERE userid=$userid"));
if($totalOnline==1){
exec_query_utf8("UPDATE tblonlineusers SET activePage='$currentURL',timestamp='$timestamp' WHERE userid=$userid");
}else{
//make another check to delete duplicate user id
if($totalOnline>1){exec_query_utf8("DELETE FROM tblonlineusers WHERE userid=$userid");}
exec_query_utf8("INSERT INTO tblonlineusers SET userid=$userid,activePage='$currentURL',timestamp='$timestamp',startOnlineTime='$timestamp'");
}

In this section, to do the data query, we need to have the database connection by doing the connection configuration. However, we recommend using MySQL UTF8 Connection Using PHP, a useful function for query the UTF8 data.

Once we have database connection, we do the data query by the following steps:

  1. Delete the expired session for the user online status
  2. Update the online information if the user is still logging in
  3. Insert new online user data row if there is a user just logged in.

If we want to return some data back to the client side, we can do the echo some data here.

Admin PHP File (a_getData.php)

$onlineUser = array();
$online_qry = exec_query_utf8("SELECT * FROM tblonlineusers");
while($online_row=mysqli_fetch_assoc($online_qry)){	
$clientName = '';$userid=$online_row['id'];
$user_qry = exec_query_utf8("SELECT * FROM tblusers WHERE id=$userid LIMIT 1");
while($user_row=mysqli_fetch_assoc($user_qry)){	
$clientName = $user_row['name'];
}
$onlineUser = array('id'=>$userid,'name'=>$clientName,'activePage'=>$online_row['activePage']);
}
echo json_encode($onlineUser);

Now we almost go reach end of the tutorial. And this section is about the php database query script for the admin system. The admin side needs only the online user status, so the above script shows the data query and assign all those data to an array variable and encode it to the Json format to return to the Ajax function. That’s it! Hope it’s helpful.

Similar Tutorials

Comments