Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: array problems

  1. #1
    New Coder
    Join Date
    Oct 2007
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    array problems

    Ok so here is my code:
    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??

  • #2
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    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.

  • #3
    New Coder
    Join Date
    Oct 2007
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That is a fair enough point. My apologies.

    Here is the full code.
    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

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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.

  • #5
    New Coder
    Join Date
    Oct 2007
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thnaks for the reply, but to be honest. I dont know what you are talking about. Can you give me an example??

  • #6
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts

    Post

    The example is your own code. Here's what your code does (some pseudo code used):
    PHP Code:
    //iterate through the log file
    foreach ($content as $dave => $value) {
        
    //assign a record to $bits
        
    $bits explode(" "$value);
    }

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

    Here's what it should do:
    PHP Code:
    //iterate through the log file
    foreach ($content as $dave => $value) {
        
    //assign a record to $bits
        
    $bits explode(" "$value);

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



  •  

    Posting Permissions

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