Page 1 of 1

Online counter

Posted: Thu Sep 22, 2005 11:16 am
by swede
can anyone see what is wrong with this code. I only get 1 visitor when I know that there are several visitors on my webbsite.

Code: Select all

<?php
 $fil = "logg.txt";
 $ip = $_SERVER['REMOTE_ADDR'];
 $tid = date('d/m H:i');
 $online = file($fil);
 $count = 1;
 for($n=0; $n < count($online); $n++){
    $file_ip = explode(" | ", $online[$n]);
    if($file_ip[0] == $tid && $file_ip[1] != $ip."\n"){
       $count++;
    }
 }
 echo " Online now: ".$count;
 ?>

Posted: Thu Sep 22, 2005 11:33 am
by shiznatix
maybe im wrong about the use of file() but dont you have to open the txt file first?

also wont the count just keep going up since i don't see how it would revert?

using sessions would be the best way to do this.

i am almost positive there is a tutorial on this in the tutorials forum. i would highly recommend reading that cause your code seams like it would not be very effective

Posted: Thu Sep 22, 2005 2:21 pm
by feyd
file() doesn't require an open file stream... however I don't see where you are writing the data to the file so I can't judge if the data inside is even correct..

Posted: Fri Sep 23, 2005 2:44 am
by swede
OK, is working now with some changes.

Posted: Fri Sep 23, 2005 3:45 am
by shiznatix
let us know how it rolls out

Posted: Fri Sep 23, 2005 4:45 am
by s.dot
i have never seen an online counter quite like that ;p but hey.. whatever works I guess.

Posted: Mon Apr 03, 2006 12:51 am
by psychotomus
that would get slow once that file turns into MB's. you should remove the IP's once there not on anymore.

Posted: Mon Apr 03, 2006 12:52 am
by feyd
old.thread.