Friday, 13 January 2017

Check how many users are logged in your website using php

Total number of users  online
Total number of users  online
Hello guys, is me again with another demanded tutorial, If you are a developer, then this is very important tutorials for you, the best way to count logged in users in php, fasting your keyboard and please keep close attention in this post.




Demo Download
So, today i am going to explain how many users are logged in to your website, I will unveil the code written in php, knowing the number of users logged in your site if you are trying to know how many users are logged in to your webpage, then you don’t need to worry anymore, is really such a nice way to tells you how many people are currently using your site, best to display this somewhere on your is just ok. the script is been test on [PHP: 5.4.7] working perfect and It is the very useful.

Now, there are few things to consider:

  •  Get the ip address of current logged in user
  •  Get current timestamp
  • Get logged in username
 This will help us to list the appropriate number of users online. 

How to get client IP address in php

One way to get user IP address is $_SERVER[REMOTE_ADDR] this track the IP address from which the user is viewing the website.
$_SERVER is a PHP super global variable which holds information about headers, paths, and script locations.


$_SERVER['REMOTE_ADDR']; // Returns the IP address from where the user is viewing the current page

With the code above you would track any IP address viewing your webpage. 

PHP time(); Function

int time ( void ) Returns the current time measured in the number of seconds
time(); example
<?php
$upWeek = time() + (7 * 24 * 60 * 60);
                   // 7 days; 24 hours; 60 mins; 60 secs
echo 'Now:       '. date('Y-m-d') ."\n";
echo 'Next Week: '. date('Y-m-d', $upWeek) ."\n";
// or using strtotime():
echo 'Next Week: '. date('Y-m-d', strtotime('+1 week')) ."\n";
?>

The above example will output something similar to:
Now:       2017-01-13
Next Week: 2017-02-06
Next Week: 2017-02-06


The function time() returns always timestamp that is timezone independent (=UTC). Check PHP Website for more info.

How to get current timestamp


<?php
$current_time=time(); // Return the current time as a Unix timestamp
echo $current;
?>

This return the current time as a Unix timestamp
In addition to format it to a date:


<?php
$current_time =time();
echo($current_time . "<br>");
echo(date("Y-m-d",$current_time));
?>

Table structure for logged in users script


CREATE TABLE IF NOT EXISTS `total_visitors` (
  `id` int(11) NOT NULL,
  `session` varchar(100) NOT NULL,
  `ip` varchar(50) NOT NULL,
  `status` int(11) NOT NULL,
  `time` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Source code for count how many users are logged.



<?php
 $current_time=time(); // Return the current time as a Unix timestamp
 $timeout = $current_time - (120); // 2min, How long ago
 $user_ip=$_SERVER['REMOTE_ADDR']; // get user ip address

if($_SESSION)
 {
 $session_exist = $db->query("SELECT session FROM total_visitors WHERE session='".$_SESSION['username']."'");
 $session_check = mysqli_num_rows($session_exist);
 if($session_check)
 {
$sql = $db->query("UPDATE total_visitors SET time='".time()."' WHERE session='".$_SESSION['username']."' ");
 }
 else{
  $db->query("INSERT INTO total_visitors values ('','".$_SESSION['username']."','".$user_ip."','1','".$current_time."')");

 }
}
 $select_total = $db->query("SELECT * FROM total_visitors WHERE time>= '$timeout' AND status=1 ");
 $total_online_visitors = mysqli_num_rows($select_total);

 $total_online = $db->query("SELECT * FROM total_visitors WHERE time>= '$timeout' ");
 $count_visitors = mysqli_num_rows($total_online);


<p>In total there is <strong><?php echo number_format( $count_visitors); ?></strong> user online :: 
<br> <strong style="color: #098;"><?php echo number_format($total_online_visitors);?> </strong>
registered and <strong><?php echo number_format($total_online_guest);?></strong> guest <br>
<b>in last 1 minutes</b>!</p>

Demo Download
Alright, see you guys later… subscribe, share with friend…

No comments:

Post a Comment

Hit me with a comment!