Skip to content

Instantly share code, notes, and snippets.

@oradwell
Created February 27, 2018 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oradwell/97a12c14312395ab825defe6c9f540b0 to your computer and use it in GitHub Desktop.
Save oradwell/97a12c14312395ab825defe6c9f540b0 to your computer and use it in GitHub Desktop.
Analytics Real-Time like Real Online Script
<?php
mysql_connect("localhost","<username>","<password>");mysql_select_db("<dbname>");
if (isset($_GET['rt']) && isset($_GET['mode'])) {
if($_GET['mode']=='count') {
$query="SELECT count(distinct userid) FROM user_online WHERE online_date>DATE_SUB(NOW(),INTERVAL 310 SECOND)";
$count=mysql_fetch_row(mysql_query($query));
echo $count[0];
} elseif ($_GET['mode']=='users') {
$query="SELECT users.username,users.userid FROM user_online,users WHERE online_date>DATE_SUB(NOW(),INTERVAL 6 SECOND) AND users.userid=user_online.userid AND user_online.userid NOT IN (SELECT userid FROM user_online WHERE online_date>DATE_SUB(NOW(),INTERVAL 310 SECOND) AND online_date<DATE_SUB(NOW(),INTERVAL 6 SECOND))";
$data=mysql_query($query);
$newcount=mysql_num_rows($data);
$var = "";
while ($info=mysql_fetch_array($data)) {
if ($var) {
$var .= ", ";
}
$var .= $info['username'];
}
if ($newcount) {
echo $var." joined.<br />";
}
$query="SELECT users.username,users.userid FROM user_online,users WHERE online_date>DATE_SUB(NOW(),INTERVAL 315 SECOND) AND users.userid=user_online.userid AND user_online.userid NOT IN (SELECT userid FROM user_online WHERE online_date>DATE_SUB(NOW(),INTERVAL 310 SECOND))";
$data=mysql_query($query);
$newcount=mysql_num_rows($data);
$var = "";
while ($info=mysql_fetch_array($data)) {
if ($var) {
$var .= ", ";
}
$var .= $info['username'];
}
if ($newcount) {
echo $var." left.<br />";
}
$query="SELECT newtable.ip,newtable.userid,newtable.url,users.username FROM (users,(SELECT * FROM user_online ORDER BY id DESC) newtable) WHERE newtable.userid=users.userid AND online_date>DATE_SUB(NOW(),INTERVAL 310 SECOND) GROUP BY newtable.userid ORDER BY newtable.url";
$data=mysql_query($query);
echo "<table border=\"1\"><caption>Online Users</caption><tr><th>#</th><th>Username</th><th>IP</th><th>URL</th></tr>";
$counter=1;
while ($info=mysql_fetch_array($data)) {
echo "<tr><td>$counter</td><td>".$info['username']."</td><td>".$info['ip']."</td><td><a href=\"".$info['url']."\">".$info['url']."</a></td></tr>";
$counter++;
}
echo "</table>";
}
}
else
{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Online Users</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script><script type="text/javascript">
setTimeout('update_onlines()',5000);
function update_onlines() {
$.get('online_users.php',{'rt':1, 'mode':'count'},function(result){
$('#user-online-count').html(result);
});
$.get('online_users.php',{'rt':1, 'mode':'users'},function(result){
$('#user-online-list').html(result);
});
setTimeout('update_onlines()',5000);
}
</script>
</head>
<body>
<?php
$query="SELECT count(distinct userid) FROM user_online WHERE online_date>DATE_SUB(NOW(),INTERVAL 310 SECOND)";
$count=mysql_fetch_row(mysql_query($query));
$onlinecount=$count[0];
$query="SELECT newtable.ip,newtable.userid,newtable.url,users.username FROM (users,(SELECT * FROM user_online ORDER BY id DESC) newtable) WHERE newtable.userid=users.userid AND online_date>DATE_SUB(NOW(),INTERVAL 310 SECOND) GROUP BY newtable.userid ORDER BY newtable.url";
$counter=1;
?>
<div style="height:3px;"></div>
Online User Count: <div id="user-online-count" style="font-size:72px"><?php echo $onlinecount ?></div>
<div id="user-online-list">
<table border="1">
<caption>Online Users</caption>
<tr><th>#</th><th>Username</th><th>IP</th><th>URL</th></tr>
<?php
$data=mysql_query($query);
while($info=mysql_fetch_array($data))
{
?>
<tr><td><?php echo $counter ?></td><td><?php echo $info['username'] ?></td><td><?php echo $info['ip'] ?></td><td><a href="<?php echo $info['url'] ?>"><?php echo $info['url'] ?></a></td></tr>
<?php
$counter++;
}
?>
</table>
</div>
<?php
}
?>
</body>
</html>
<html>
<head></head>
<body>
<p>This is our front site what visitors see</p>
<script type="text/javascript">
function uopPost(){
$.post("http://www.example.com/uop.php",{
'uid':'123',
'url': document.location.href
},
{
}
);
setTimeout("uopPost()",300000);}
uopPost();
</script>
</body>
</html>
<?php
ob_start();
if (empty($_POST['uid']) || empty($_POST['url'])) {
header("Location: /");
ob_end_flush();
} else {
ob_end_flush();
mysql_connect("localhost","<username>","<password>"); mysql_select_db("<dbname>"); $uid=$_POST['uid'];
$url=$_POST['url'];
$ip=$_SERVER['REMOTE_ADDR'];
mysql_query("INSERT INTO user_online (`userid`,`ip`,`url`) VALUES ('".$uid."','".$ip."','".$url."')");
}
CREATE TABLE IF NOT EXISTS `user_online` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userid` bigint(20) NOT NULL,
`ip` varchar(15) CHARACTER SET latin1 NOT NULL,
`url` text NOT NULL,
`online_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
@oradwell
Copy link
Author

DO NOT ACTUALLY USE THIS. VERY OLD AND VULNERABLE TO SQL INJECTION.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment