Selasa, 11 September 2012

0 Feature User Online Page for Dabr (Twitter Client)


How to make it?
Create a file (eg: count.php):
code:
<?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);
?>
We have to include the count.php file to all the accessible pages.
In dabr, usually common/theme.php -> function theme_page()
code:
if (user_is_authenticated()) {
require_once("count.php");
}
echo ‘</body>
</html>’;
exit();
Create another file (eg: online.php). This file is to show the list of the online users.
Code:
<?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 .= $array[0] ."</a><br />";
}
$content .= "</div>";
?>
And edit the index.php file to add this line
code:
<?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:
code:
<?php
function online_page() {
require_once("online.php");
theme(‘page’, ‘Online Users’, $content);
}
?>
And don’t forget to chmod 777 the file onlineusers.txt…

0 Comments

Bagaimana Pendapat Anda ?