Skip to content

Instantly share code, notes, and snippets.

@dejurin
Forked from inanimatt/index.html
Created Nov 11, 2015
Embed
What would you like to do?
Count online users
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Pinger test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<h1>Online user counter</h1>
<p id="counter">Users online: <span id="userCount">loading…</span></p>
<script type="text/javascript" charset="utf-8">
$().ready(function (){
var token = '';
var pinger = setInterval(function (){
$.ajax({
cache: false,
data: {
token: token,
},
timeout: 2500,
type: 'GET',
url: 'pinger.php',
dataType: 'json',
success: function (data, status, jqXHR){
$('#userCount').text(data.userCount);
token = data.token;
}
});
}, 5000);
});
</script>
</body>
</html>
<?php
// Look for token
$token = (isset($_GET['token']) && preg_match('/^[0-9a-f]{8}$/', $_GET['token'])) ? $_GET['token'] : false;
if (!$token) {
$token = sprintf('%08x', crc32(microtime()));
}
// get current minute, build APC key
$quadrant = ceil(date_create()->format('s') / 15); // between 1-4
$previousQuadrant = $quadrant - 1 < 1 ? 4 : $quadrant - 1;
$key = 'pinger_'.$quadrant;
$previousKey = 'pinger_'.$previousQuadrant;
// get tokens for the last 30 seconds
$current = apc_fetch($key);
$previous = apc_fetch($previousKey);
if (!is_array($current)) {
$current = array();
}
if (!is_array($previous)) {
$previous = array();
}
// Add current token if not found
if (count($current) < 250 && !in_array($token, $current)) {
$current[] = $token;
apc_store($key, $current, 31);
}
// Build return object: userCount, token
$output = array(
'userCount' => count($current) > 250 ? '250+' : count(array_unique(array_merge($current, $previous))),
'token' => $token,
);
header('Content-Type: application/json');
print json_encode($output);
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment