Prova con questa classe ^^
Codice PHP:
<?php
if(!function_exists('file_get_contents')) {
function file_get_contents($file, $include_path) {
return @implode("", @file($file, $include_path));
}
}
class usersOnline {
var $users_file;
var $timeout;
var $timestamp;
var $ip;
function usersOnline($file_, $timeout_) {
global $_SERVER;
$this->timestamp = time();
$ip_ = ($_SERVER['REMOTE_ADDR'] != "" && preg_match("/^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/", $_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : "non rilevato";
$this->ip = $ip_;
$this->users_file = $file_;
$this->timeout = $timeout_;
$this->newUser();
$this->deleteUser();
}
function newUser() {
$content = file_get_contents($this->users_file);
$lines = file($this->users_file);
foreach($lines as $line) {
$linea = explode("|", $line);
if($linea[0] == $this->ip) {
return;
}
}
$ip = $this->ip;
$timestamp = $this->timestamp;
$content .= "\n$ip|$timestamp";
$this->writeFile($this->users_file, trim($content));
}
function deleteUser() {
$file = file($this->users_file);
foreach($file as $key => $line) {
$linea = explode("|", $line);
$aa = $this->timestamp - $this->timeout;
if($linea[1] < $aa && $linea[0] != $this->ip) {
unset($file[$key]);
}
}
$this->writeFile($this->users_file, trim(str_replace("\n\n", "\n", implode("\n", $file))));
}
function countVisits() {
return count(file($this->users_file));
}
function writeFile($file, $content) {
$fp = @fopen($file, "w");
flock($fp, LOCK_EX);
fwrite($fp, $content);
flock($fp, LOCK_UN);
fclose($fp);
}
}
?>
l'ho scritta io :O
Per usarla fai così:
Codice PHP:
<?php
require ("file_della_classe.class.php");
$users_online = new usersOnline("online.txt", 3*60);
$count = $users_online->countVisits();
echo $count > 1 ? "Ci sono $count visitatore online." : "Abbiamo $count visitatore online.";
?>
dove "online.txt" è il file degli utenti e "3*60" è il timeout (che 3x60 => 3 minuti)
Ciao