1

i have written a script to output active users on my site....

part of this is counting unique ips in the log, as the array i use to split the lines / data unloads active users from the array list after 5 minutes.....

however the "3 online users now" count is not working properly.....

it kinda works.... when someone views a page, it says there is 1 user

lets say i view a page.... 1 visitor

then user 2 views a page .... 2 visitors

but if i then view another page, it displays 3 users.....

even though i use the same ip for both page requests....

here is my code

$data = file_get_contents('active-log.txt');

$break = "\r\n";

$lines = explode($break, $data);

foreach ($lines as $key => $value) {

$active_ip[] = $lines[$key][1];

}

$active_ip_count = array_unique($active_ip);

$active_users = (count($active_ip_count));

$active_users is the variable i use to display how many unique visitors are online at one time

thanks in advance for anyone that can help me thanks

....

EDIT

.....

here is a sample of the log saved....

 1328469393|157.55.39.84|g-book
 1328469398|157.55.39.84|downloads
 1328469400|157.55.39.84|badger
 1328469404|157.55.39.84|home
 1328469408|157.55.39.84|boneyard-dogs

the first part is timestamp (to remove the line from array, if timestamp is older than 5 minutes... this works fine)

the second part is ip

third part is page viewed and the new line is created with \r\n

$lines[$key][1] is the variable for each ip in each line....

as im not exacly a php expert, when writing scripts, i test them heavily while developing, and each time i add a new line of script , i echo the data, to check its what i hope, to make sure i make no mistakes......

here is a section of code that i didnt paste as i didnt think it was necessary....

foreach($lines as $k=>$v) {

$lines[$k] = explode("|", $v); }

// echo $lines[0][0]; // now this is first array of first line .... line 2 / url would be - $lines[1][2]

this is in my code, straight after the line "$lines = explode($break, $data);" in my code

DJ-P.I.M.P
  • 105
  • 2
  • 6
  • 15

1 Answers1

1

Have you looked at the output of var_dump($active_ip) after the foreach loop ends? With this setup, I'm pretty sure $lines[$key][1] is simply the first character of the line you're dealing with, so that's not going to work well for a number of reasons. What does active-log.txt look like? Does it only contain IP addresses or user names, too? If it only contains IP addresses, consider using something like this:

<?php
$data = file('active-log.txt');
$no_duplicate_ips = array_unique($data);
$active_users = (count($no_duplicate_ips));
?>

Edit:

Right, that makes sense then. Try this:

$data = file_get_contents('active-log.txt');
$break = "\r\n"; //Note that it's generally a good idea to use PHP_EOL throughout your code, for greater cross-platform compatibility
$lines = explode($break, $data);

$exploded_data = array();
$active_ips = array();

foreach($lines as $v) {
    $exploded_data = explode("|", $v); 
    //Now check whether the timestamp is not > 5 min
    if(TIMESTAMP CHECK HERE) { 
         //OK, this one is not too old
         $active_ips[] = $exploded_data[1]; 
    }
}

$active_ip_count = array_unique($active_ip);
$active_users = (count($active_ip_count));
Daan
  • 3,403
  • 23
  • 19
  • i cant just do an array_unique() on the full log file, as there is a section of code to remove the log line from the array if the time is past 5 minutes.... it is still in the log file, its just unset from the array, so i need to do the count of ips that are at location $lines[$key][1] – DJ-P.I.M.P Feb 05 '12 at 19:28
  • do you know if i can use a statement like this? .... if("$var1 = $var2" || "$var3 = $var4") { code to be executed; } – DJ-P.I.M.P Feb 05 '12 at 20:27
  • see above for a solution that may be easier to understand than the one I just posted in the comments (this one was too long). – Daan Feb 05 '12 at 20:27
  • you'd have to use something like if($var1 == $var2 || $var3 == $var4) { code to be executed; } because = assigns, == compares. – Daan Feb 05 '12 at 20:28