Online counter

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
swede
Forum Newbie
Posts: 3
Joined: Thu Sep 22, 2005 11:12 am

Online counter

Post 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;
 ?>
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
swede
Forum Newbie
Posts: 3
Joined: Thu Sep 22, 2005 11:12 am

Post by swede »

OK, is working now with some changes.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

let us know how it rolls out
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

i have never seen an online counter quite like that ;p but hey.. whatever works I guess.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post by psychotomus »

that would get slow once that file turns into MB's. you should remove the IP's once there not on anymore.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

old.thread.
Post Reply