Undefined offset issue. Please help!

Hi all,

I’m pretty sure this issue is caused by the last iteration of my loop through a log file, which is simply a blank line, which makes the data[0] [1] [2] etc… throw up this issue. I’m reading through a log file to generate statistics.

I’m very new to PHP so need things explained in simplistic terms. I’m not supposed to edit the file at all but did so simply to see that removing the last empty line fixed the issue, which it did.

Each line of the file I’m reading is as follows: 82.100.77.183 – [2007-05-31 07:16:22] “GET index.php HTTP/1.0” 200 3836 “MSIE 7.0”

Any simple solutions for this?? How about if I define data in some way before the ‘explode’? Would this affect how I need to structure it??

And here’s my loop:

//Open file and process contents.
		$handle = fopen($subdir.'/'.$file, 'r');
		while (!feof($handle)){
			//add the next line to the variable $line
			$line = fgets($handle, 1024);
			//work with the string here to get the data I need
			$data = explode ('"',$line);
			
			/**
			* data[1] contains "GET dir/filename ......" search this for 'articles'
			* data[2] contains e.g. "404 12729" explode again with ' ' to seperate
			* this into status_code and bandwidth values.
			*/
			$pos = strpos($data[1],$articles);
			if ($pos !== false){
				//articles found
				$art_req++;
			}
			
			//Seperate data[2] to get the status code and the bandwidth used
			$trimmed = trim($data[2]);
			$sep_data = explode (' ',$trimmed);
			
			//$sep_data[0] should contain the status_code and $sep_data[1] the bandwidth
			$status_code = $sep_data[0];
			$bw_data = $sep_data[1];
			
			//Bandwidth calculation
			$bw_total += $bw_data;
			
			//404 error count
			if ($status_code == 404){
				$error_count++;
			}
			//Counting the number of requests
			$requests++;
		}
		//This area executes once between the two files
		//close file
		fclose ($handle);
		
		//Calculate bandwidth in kb and round to 2 decimal places
		$bw_total = $bw_total/1024;
		$bw_total = round($bw_total, 2);
		
		$months[$file_id] .= $requests. ':' .$art_req. ':' .$bw_total. ':' .$error_count;
		
		$file_id = 'May';
	}
	
	//Close directory
	closedir($dh);

Any help greatly appreciated

Thank You

If you could post the content of the file ($file) that would help us in test out your script.

A link to the file will do too.

if(count($data) == 1) { continue; }

I see why they call you the Mentor.

I should have read the code more carefully. I was thinking a block of 1024 bytes data was read in each loop like fread(). :blush: