Click to See Complete Forum and Search --> : Users Online In Past Hour


Jick
08-06-2003, 11:46 AM
I have this script here that I found:
<?php

$timer = 60; // Timeout - After this time the users will be deleted (in minutes).
$filename = "log_past.txt"; // Name of the file where all the data about the user's activity will be saved.

if (!$datei) $datei = dirname(__FILE__)."/$filename";
$time = @time();
$ip = $REMOTE_ADDR;
$string = "$ip|$time\n";
$a = fopen("$filename", "a+");
fputs($a, $string);
fclose($a);

$timeout = time()-(60*$timer);

$all = "";
$i = 0;
$datei = file($filename);
for ($num = 0; $num < count($datei); $num++) {
$pieces = explode("|",$datei[$num]);

if ($pieces[1] > $timeout) {
$all .= $pieces[0];
$all .= ",";
}
$i++;
}

$all = substr($all,0,strlen($all)-1);
$arraypieces = explode(",",$all);
$useronline = count(array_flip(array_flip($arraypieces)));

// display how many people where activ within $timeout
echo $useronline;

// Delete
$dell = "";
for ($numm = 0; $numm < count($datei); $numm++) {
$tiles = explode("|",$datei[$numm]);
if ($tiles[1] > $timeout) {
$dell .= "$tiles[0]|$tiles[1]";
}
}

if (!$datei) $datei = dirname(__FILE__)."/$filename";
$time = @time();
$ip = $REMOTE_ADDR;
$string = "$dell";
$a = fopen("$filename", "w+");
fputs($a, $string);
fclose($a);
?>
The place where I got it said that this script will tell how many users have been on in the past ## minutes. I have is set to 60 as you can see so it will show how many users have been on in the past hour. It writes the info to a log file called "log_past.txt". It adds a new line in the log file for each IP. Everything looks like it's working correctly but it seems that in the log file it's adding a new line for my IP every time I refresh the page. I want it so it only puts my IP in the file once. Can someone provide a peice of code I could add to this script to prevent it from adding my IP every time I refresh? Thanks. :)

pyro
08-06-2003, 12:23 PM
The code I gave you in responce to a very similar questions should do exactly what you need, if you change the time to 60 minutes...

Jick
08-06-2003, 03:20 PM
Ok I did a search on the forum and this is what I got:
if (!isset($_COOKIE["cookiename"])) { #if cookie is not set
#write log file
$fp = fopen($logfile, "a");
fwrite ($fp, $logdata);
fclose ($fp);
setcookie ("cookiename", "visited", time() +"1200"); #set for 20 minutes (time is in seconds)
}
Now I need to know if there is any thing I should change or add to this. Also where exactally do I put it? I want the script I posted in my last post to still work the same exact way but just make it so you can't add another line by just refreshing the page. Thanks. :D