Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8836689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:33:11+00:00 2026-06-14T09:33:11+00:00

I have a server log file from which I am trying to create a

  • 0

I have a server log file from which I am trying to create a PHP page which summaries the data it stores. Each record in the log is stored on a new line, and in the format:

207.3.35.52 -- [2007-04-01 01:24:42] "GET index.php HTTP/1.0" 200 11411 "Mozilla/4.0"

//ip -- [timestamp] "GET url HTTP/1.0" status code bytes "user agent".

I am trying to write a summary which displays: the total amount of requests, the total amount of requests form the articles directory, the total bandwidth consumed and finally the amount of 404 errors and their pages.

PHP:

$handle = fopen('logfiles/april.log','r') or die ('File opening failed');
$requestsCount = 0;
    while (!feof($handle)) {
        $dd = fgets($handle);
        $requestsCount++;   
        $parts = explode('"', $dd);
        $statusCode = substr($parts[2], 0, 4);
        }
fclose($handle);

This code opens the file and counts the amount of records, seperates and finds the status code number in the record. When echoing $statusCode it does show the correct information, showing all the status codes in the log.

A function which accepts two arguements to total the 404 errors:

function requests404($l,$s) {
        $r =  substr_count($l,$s);
        return "Total 404 errors: ".$r."<br />";
}

Echo the result:

echo requests404($statusCode, '404');

This function doesn’t work, it just returns 0. Working with txt files in PHP is my weakest skill and I would really appreciate some help as I think I may be going about it the complete wrong way. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:33:13+00:00Added an answer on June 14, 2026 at 9:33 am
    $handle = fopen('logfiles/april.log','r') or die ('File opening failed');
    $requestsCount = 0;
    $num404 = 0;
    
    while (!feof($handle)) {
        $dd = fgets($handle);
        $requestsCount++;   
        $parts = explode('"', $dd);
        $statusCode = substr($parts[2], 0, 4);
        if (hasRequestType($statusCode, '404')) $num404++;
    }
    
    echo "Total 404 Requests: " . $num404 . "<br />";
    fclose($handle);
    
    function hasRequestType($l,$s) {
            return substr_count($l,$s) > 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have been trying to write an awk script which processes a log file, but
i was trying to enable slow_query_log. i have downloaded my.cnf file from server using
I have a log file containing statistics from different servers. I am separating the
I have a directory /tmp/dir with two types of file names /tmp/dir/abc-something-server.log /tmp/dir/xyz-something-server.log ..
I have a file called error.log on my server that I need to frequently
In the apache server installation I have a .conf file which forwards all requests
I'm trying to implement a function which would load a server-stored CSV file, and
I am trying to download a file from a server to an iphone like
Running sql server 2005 I have database A. I am trying to restore from
I have developed a servlet that outputs the server.log file of my JBoss 7.1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.