/*
*Create a file on the dabr root folder (eg: count.php):
*/
<?php
error_reporting(E_ERROR | E_PARSE);
$dataFile = "onlineusers.txt";
if (user_is_authenticated()) {
$user = user_current_username();
}
// this is the time in **minutes** to consider someone online before removing them from our file
// berapa menit tenggang waktu yg dibutuhkan untuk tahu user masih online atau tidak.
$sessionTime = 5;
if(!file_exists($dataFile)) {
$fp = fopen($dataFile, "w+");
fclose($fp);
}
$users = array();
$onusers = array();
// check up
$fp = fopen($dataFile, "r");
flock($fp, LOCK_SH);
while(!feof($fp)) {
$users[] = rtrim(fgets($fp, 32));
}
flock($fp, LOCK_UN);
fclose($fp);
// clean up
$x = 0;
$alreadyIn = FALSE;
foreach($users as $key => $data) {
list(,$lastvisit) = explode("|", $data);
if(time() - $lastvisit >= $sessionTime * 60) {
$users[$x] = "";
} else {
if(strpos($data, $user) !== FALSE) {
$alreadyIn = TRUE;
$users[$x] = "$user|" . time(); //updating
}
}
$x++;
}
if($alreadyIn == FALSE) {
$users[] = "$user|" . time();
}
// write up
$fp = fopen($dataFile, "w+");
flock($fp, LOCK_EX);
$i = 0;
foreach($users as $single) {
if($single != "") {
fwrite($fp, $single . "\\r\\n");
$i++;
}
}
flock($fp, LOCK_UN);
fclose($fp);
?>
//Create another file (eg: online.php). This file is to show the list of the online users.
<?php
$myFile = "onlineusers.txt";
$fsc = file($myFile);
$lines = count(file($myFile));
$content = "<div>".$lines." Online Users:<br />";
foreach($fsc as $line) {
$array = explode("|", $line);
$content .= "<a href=\\"user/".$array[0]."\\">".$array[0]."</a><br/>";
}
$content .= "</div>";
?>
//We have to include the count.php file to all the accessible pages.
//In dabr, usually common/theme.php -> function theme_page()
....
if (user_is_authenticated()) {
require_once("count.php");
}
echo \'</body>
</html>\';
exit();
And edit the index.php file to add this line
<?php
menu_register(array (
......
\'online\' => array (
\'security\' => true,
\'callback\' => \'online_page\',
),
....
));
?>
//Still at the index.php file just before the browser_detect(); add this line:
<?php
function online_page() {
require_once("online.php");
theme(\'page\', \'Online Users\', $content);
}
?>
//And don\'t forget to chmod 777 the file onlineusers.txt...
//http://dev.writelonger.com/onlineuser.html