...

View Full Version : array problems



eon201
11-06-2007, 03:16 PM
Ok so here is my code:


// calculate all page requests
$all_request = count($log_request); // calculate all page counts

//calculate dirty page counts(eg gif,jpg,txt,swf,etc)
$bad_request = substr($bits[5], -3, 3);

// calculate overall page count
// $good_request = $all_request - $bad_request;

// echo "$good_request";


So what I am trying to do is build a simple url visited counter. i know you canget free ones, but there you go! At the minute its calculating the whole number of values within the array. But when I try to use the substr function to try and work out how many of the values end in jpg, or swf etc I cant egt it to work! Can anyone help??

aedrin
11-06-2007, 03:51 PM
Have you actually run this?

I can't make heads or tails of this. You should post your entire code if you're not sure what you are doing.


But when I try to use the substr function to try and work out how many of the values end in jpg, or swf etc I cant egt it to work!

It doesn't work?

What doesn't work?

What errors are you getting?

What are you expecting to happen?

Etc.

We cannot read minds. And we will not guess.

eon201
11-06-2007, 04:12 PM
That is a fair enough point. My apologies.

Here is the full code.


<?php
// Work out our time and when we search to
$timenow = strtotime("now");
$endtime = strtotime("-1 hour");

echo $timenow . "<br />";
echo $endtime . "<br />";
echo "====================+" . "<br />";

$filename = 'logs/'. 'ex'. date('ymd'). '.log'; // Filename Checker

if (file_exists($filename)) // Does the corresponding filename exist - if yes then
{
$fp = fopen($filename, "r"); //Open the server log
$content = fread($fp, filesize($filename)); // Read the server log

$content = explode("\n", $content);

// $content is the array
// $dave is a name of a variable to store the key in
// $value is a name of a variable to store the value in
foreach($content as $dave=>$value) {
$firstcharacter = substr($value, 0, 1);
if($firstcharacter == "#" || $firstcharacter == "") {
}
else {
$bits = explode(" ", $value);

// CLIENT IP
// $bits[9]
$log_clientIP[] = $bits[9];

// CLIENT REQUEST
// $bits[5]
$log_request[] = $bits[5];

}
}

fclose($fp);

// Counts unique values for ip's
echo '<br/><br/>';
$tlog_clientIP = count(array_unique($log_clientIP)) . "<br /><br/>";
echo "Unique Visitors = $tlog_clientIP";

// Counts page requests
echo '<br/><br/>';

// calculate all page requests
$all_request = count($log_request); // calculate all page counts value

//calculate dirty page counts
$lastthree = substr($bits[5], -3, 3);
if($lastthree == "gif" || $lastthree == "jpg" || $lastthree == "txt" || $lastthree == "swf" || $lastthree == "png" || $lastthree == "css" || $lastthree == "/^.js/" || $lastthree == "ico"){

//add all of these together to get the value of $bad_request
}
else {
}


// calculate actual page count value
//$good_request = $all_request - $bad_request;

//echo "$good_request";

// Calculate avg ppv
echo '<br/><br/>';
$overallpages = count($log_request);
$tavgppv = $overallpages/$tlog_clientIP;// This needs to be changed to $good_requests
echo "Avg PPV = $tavgppv";

// Calculate top 5 page requests
echo '<br/><br/>';
print_r(array_count_values($log_request));
}



else // Does the corresponding filename exist - if no then
{
echo 'error!! this file does not exist';
}
?>

I am pulling in a server log file and creating it into an array. The part I am having trouble with is calculating the actual page view count (as the server not only logs .php,.html files but also, gif, jpeg,txt,swf etc), by working out the overall count $overallpages and then working out the overall count for the file types that i wish to not be included (the dirty page count') and taking the latter away from the first. The only problem is. I have no idea how to do this!

I hope that is a better explanation.

Thanks. eon201

Fumigator
11-06-2007, 05:33 PM
The variable $bits is assigned a value inside your foreach() loop, which means it is assigned different values over and over and over again. But then you attempt to use the value of $bits[5] after the foreach loop is over, at which point the value of $bit is the last thing that was processed inside the loop.

You probably want to move the check for swf, etc to inside the loop and tally up $badrequest there.

eon201
11-06-2007, 05:37 PM
thnaks for the reply, but to be honest. I dont know what you are talking about. Can you give me an example??

Fumigator
11-06-2007, 05:49 PM
The example is your own code. Here's what your code does (some pseudo code used):


//iterate through the log file
foreach ($content as $dave => $value) {
//assign a record to $bits
$bits = explode(" ", $value);
}

if ($bits[5] ends in 'gif', 'jpg', 'swf', 'txt', etc.etc.) {
$badrequest++;
}


Here's what it should do:


//iterate through the log file
foreach ($content as $dave => $value) {
//assign a record to $bits
$bits = explode(" ", $value);

if ($bits[5] ends in 'gif', 'jpg', 'swf', 'txt', etc.etc.) {
$badrequest++;
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum