SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    May 2011
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:

    PHP Code:
    //Open file and process contents.
            
    $handle fopen($subdir.'/'.$file'r');
            while (!
    feof($handle)){
                
    //add the next line to the variable $line
                
    $line fgets($handle1024);
                
    //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_total2);
            
            
    $months[$file_id] .= $requests':' .$art_req':' .$bw_total':' .$error_count;
            
            
    $file_id 'May';
        }
        
        
    //Close directory
        
    closedir($dh); 
    Any help greatly appreciated



    Thank You

  2. #2
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.

  3. #3
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    if(count($data) == 1) { continue; }
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  4. #4
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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().


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •