I want to read log file of the site in php

Discussion in 'PHP' started by Subikar, May 30, 2007.

  1. #1
    Hi friend I want to read log files and want the log file data to populate in database. Is there any class or function available? so I can get help from there.
     
    Subikar, May 30, 2007 IP
  2. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #2
    do you mean access logs ???

    do you use cpanel, and what format are you keeping logs in, post a couple of lines from the top and the bottom of the log and I'll write something to turn it into an associative array, you have about 20 minutes untill I gotta go out, so b quick.
     
    krakjoe, May 31, 2007 IP
  3. Subikar

    Subikar Active Member

    Messages:
    241
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #3
    This one is upper portion
    It has more then 8 lakhs lines I want to retrieve all and udate it into database.


    This one is lower portion
     
    Subikar, May 31, 2007 IP
  4. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #4
    
    function LogToArray( $log )
    {
    	if( file_exists( $log ) )
    	{
    		foreach( file( $log ) as $number => $line )
    		{
    			if( trim( $line ) )
    			{
    				preg_match( '~([0-9\.]+) - - \[(.*?[^\]])\] "([A-Z]+) (.*?[^ ]) (.*?[^"])" ([0-9]+) ([0-9]+)~si',
    						$line,
    						$request
    				);
    				if( $request[0] )
    				{
    					$requests[ $number ] = array(
    						'REMOTE_ADDR' => $request[1],
    						'REQUEST_TIME' => $request[2],
    						'REQUEST_METHOD' => $request[3],
    						'REQUEST_URI' => $request[4],
    						'REQUEST_HTTP_VERSION' => $request[5],
    						'REQUEST_RESPONSE' => $request[6],
    						'REQUEST_LENGTH_SENT' => $request[7]
    					); 
    				}
    					
    			}		
    		}
    		return $requests ;	
    	}
    }
    function SetupDataBase( $dblink )
    {
    	return mysql_query( 'CREATE TABLE `requests` (
    						`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    						`REMOTE_ADDR` VARCHAR( 15 ) NOT NULL ,
    						`REQUEST_TIME` TEXT NOT NULL ,
    						`REQUEST_METHOD` VARCHAR( 10 ) NOT NULL ,
    						`REQUEST_URI` TEXT NOT NULL ,
    						`REQUEST_HTTP_VERSION` TEXT NOT NULL ,
    						`REQUEST_RESPONSE` TEXT NOT NULL ,
    						`REQUEST_LENGTH_SENT` INT NOT NULL
    						) ENGINE = MYISAM ;', $dblink 
    	);
    }
    function LogToDb( $log, $dblink )
    {
    	foreach( LogToArray( $log ) as $num => $data )
    	{
    		if( !mysql_query( sprintf( 'INSERT INTO `requests` VALUES("", "%s", "%s", "%s", "%s", "%s", "%s", "%s")',
    									$data['REMOTE_ADDR'],
    									$data['REQUEST_TIME'],
    									$data['REQUEST_METHOD'],
    									$data['REQUEST_URI'],
    									$data['REQUEST_HTTP_VERSION'],
    									$data['REQUEST_RESPONSE'],
    									$data['REQUEST_LENGTH_SENT'] ), $dblink  ) )
    		{
    			return !die( sprintf(
    				'Cannot query database mysql said %s',
    				mysql_error( $dblink  )
    			) );
    		}
    	}	
    	print( 'Log parsed and inserted into database<br />' );	
    }
    function DbToScreen( $dblink )
    {
    	if( ! ( $result = mysql_query( 'SELECT * FROM `requests`' )) )
    	{
    		return !die( sprintf( 
    			'Cannot query database for requests'
    		) );
    	}
    	else
    	{
    		print( '<h2>Log Table</h2>' );
    		print( '<table>' );
    		print( '<tr>' );
    		print( '<th>Remote Address</th>' );
    		print( '<th>Time</th>' );
    		print( '<th>Method</th>' );
    		print( '<th>URI</th>' );
    		print( '<th>HTTP Version</th>' );
    		print( '<th>Response</th>' );
    		print( '<th>Length Sent</th>' );
    		print( '</tr>' );
    		print( '<tr>' );
    		while( $assoc = mysql_fetch_assoc( $result ) )
    		{
    			print( '<tr>' );
    			printf( '<td>%s</td>', $assoc['REMOTE_ADDR'] );
    			printf( '<td>%s</td>', $assoc['REQUEST_TIME'] );
    			printf( '<td>%s</td>', $assoc['REQUEST_METHOD'] );
    			printf( '<td>%s</td>', $assoc['REQUEST_URI'] );
    			printf( '<td>%s</td>', $assoc['REQUEST_HTTP_VERSION'] );
    			printf( '<td>%s</td>', $assoc['REQUEST_RESPONSE'] );
    			printf( '<td>%s</td>', $assoc['REQUEST_LENGTH_SENT'] );
    			print( '</tr>' );	
    		}
    		print( '</tr>' );
    		print( '</table>' );
    	}	
    }
    
    PHP:
    First do

    
    $dblink = mysql_connect('localhost', 'root', '');
    mysql_select_db( 'logs', $dblink );
    SetupDataBase( $dblink );
    
    PHP:
    then

    
    $dblink = mysql_connect('localhost', 'root', '');
    mysql_select_db( 'logs', $dblink );
    LogToDb( 'log.txt', $dblink );
    
    PHP:
    then

    
    $dblink = mysql_connect('localhost', 'root', '');
    mysql_select_db( 'logs', $dblink );
    DbToScreen( $dblink );
    
    PHP:
    have fun
     
    krakjoe, May 31, 2007 IP
  5. Subikar

    Subikar Active Member

    Messages:
    241
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #5
    My file is more then 8lakhs line so it is not able retrieve.
    It is showing an error
     
    Subikar, May 31, 2007 IP
  6. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #6
    
    function LogToArray( $log )
    {
        if( file_exists( $log ) and ( $log = fopen( $log, 'r' ) ) )
        {
        	while( !feof( $log ) )
        	{
                if( preg_match( '~([0-9\.]+) - - \[(.*?[^\]])\] "([A-Z]+) (.*?[^ ]) (.*?[^"])" ([0-9]+) ([0-9]+)~si',
                            fgets( $log, 4096 ),
                            $request
                ) )
                {
                    $requests[ ] = array(
                        'REMOTE_ADDR' => $request[1],
                        'REQUEST_TIME' => $request[2],
                        'REQUEST_METHOD' => $request[3],
                        'REQUEST_URI' => $request[4],
                        'REQUEST_HTTP_VERSION' => $request[5],
                        'REQUEST_RESPONSE' => $request[6],
                        'REQUEST_LENGTH_SENT' => $request[7]
                    ); 
                }
    		}
    		fclose( $log );
            return $requests ;  
        }
    }
    
    PHP:
     
    krakjoe, May 31, 2007 IP
  7. Subikar

    Subikar Active Member

    Messages:
    241
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #7
    Sorry again the same error is giving while running the file.

    I am trying to retrieve more then 8000 hundred lines of data from a file
     
    Subikar, May 31, 2007 IP
  8. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #8
    It really shouldnt matter if it only retrieves one line at a time, theres not much chance that one line can be over 8 megs
     
    krakjoe, Jun 4, 2007 IP