Hi,
I'm having a problem with array_search(). Here's what I'm trying to do:
I have a list of IP addresses that I know are from people who have clicked through to our website. I'm trying to determine what (if any) orders they have made whilst on the website - I know that at least some of them have made a purchase (the list of IP addresses is quite large). I'm trying to compare each line of the IP address file with each entry in our IIS logfiles using PHP. For reference IIS is set to log entries daily and the logfiles are in the following format:
date time cs-method cs-uri-stem cs-uri-query cs-username c-ip cs-version cs(User-Agent) cs(Referer) sc-status sc-bytes
Here is the code:
<?php
// Get the month
fwrite(STDOUT, "Please enter the month to process and press enter:\r\n");
$month = trim(fgets(STDIN));
// Get the year
fwrite(STDOUT, "\r\n\r\nPlease enter the year to process and press enter:\r\n");
$year = trim(fgets(STDIN));
// If month is prior to October, add a leading 0 to help with filenames
if ($month < 10) {
$month = "0$month";
}
// Split the year value to help with IIS filenames
$pyear = str_split($year,2);
// Load each line of the IP address file into an array
$prunner = file("c:\\path\\to\\file\\file-$month-$year.txt",FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$i = 1;
$mday = 31;
// Initialise a variable for counting possible sales
$scount = 0;
while ($i <= $mday) {
// Process the IIS log file where the day = $i
if ($i < 10) {
$day = "0$i";
} else {
$day = $i;
}
// Check the file exists
$logfile = "c:\\path\\to\\iislogs\\ex$pyear[1]$month$day.log";
if (file_exists($logfile)) {
// Open the file for reading and process each line
$iisdata = fopen($logfile,"r");
while (!feof($iisdata)) {
$iis = fgets($iisdata);
// If the line is a comment, ignore it (commented lines begin with a #)
//
if (strpos($iis,"#")!==0){
// Split the line from the IIS log file into it's component values
$elements = explode(" ",$iis);
// Search the current IP address value in the IIS log file against the list
// of IP addresses in the $prunner array
if(array_search($elements[6],$prunner)==!false) {
// Now we need to check if the matching entry requested the thank you page.
// If it did we will increment $scount by 1 and grab the orderid
if(substr($elements[4],0,28) == "mode=order_message&orderids=") {
$scount++;
$oidparts = explode("=",$elements[4]);
$oid = $oidparts[3];
// Connect to database and query the value of the order
$conn = mysql_connect("127.0.0.1","user","pass");
if(!$conn){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("dbname",$conn);
$query = "SELECT total FROM orders WHERE orderid = $oidparts[3]";
$ovalue = mysql_query($query,$conn);
$oidfile = fopen("c:\\path\\to\\file\\orderids.txt","a");
fwrite($oidfile,"$ovalue\r\n");
fclose($oidfile);
}
}
}
}
// Close the logfile
fclose($iisdata);
} else {
// Warn if log file does not exist
echo "\r\n\r\nThe log file $logfile does not exist.\r\n\r\n";
sleep(3);
}
$i++;
}
// Calculate value of orders
$sales = file("c:\\path\\to\\file\\orderids.txt",FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$salestotal = array_sum($sales);
$output = fopen("c:\\path\\to\\file\\results.txt","r");
fwrite($output,"\r\nSales:$scount\r\nTotal:£$salestotal");
$fclose($output);
// Wait and exit
sleep(30);
exit(0);
?>
I'm not seeing any PHP errors but I'm also not logging any sales, even though I know some of the IP's belong to users who have made purchases. I think I've made a logical error somewhere in the main loop.
Also, this script seems quite inefficient. Although the list of IP addresses is large and our logfiles are quite big (each daily log is about 100M😎 I was surprised to find that it takes around one hour to process each IIS logfile. Any hints for boosting the performance of this script would be greatly appreciated.
EDIT: I'm pretty sure the problem lies with how I'm using array_search()