Open In App

How to find the total number of HTTP requests ?

Last Updated : 18 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will find the total number of HTTP requests through various approaches and methods like implementing server-side tracking by using PHP session variables or by analyzing the server logs to count and aggregate the incoming requests to the web application. By finding the total count of HTTP requests we can understand the web application metrics to optimize the application.

There are two approaches through which we can find the total number of HTTP requests.

Approach 1: Using Session Variable

In this approach, we are using the session variables to count and store details of each HTTP request, which includes the request method, time, and the IP address of the client. We are displaying the total number of requests and if there is more than one request then the list of detailed information for each of the requests is shown in the list bullet manner.

Example: In the below example, we will find the total number of HTTP requests in PHP using session variables.

PHP




<?php
session_start();
  
// Increasing the request count
if (!isset($_SESSION["request_count"])) {
    $_SESSION["request_count"] = 1;
} else {
    $_SESSION["request_count"]++;
}
  
// Storing details about the current request
$requestDetails = [
    "method" => $_SERVER["REQUEST_METHOD"],
    "timestamp" => date("Y-m-d H:i:s"),
    "ip" => $_SERVER["REMOTE_ADDR"],
];
if (!isset($_SESSION["request_details"])) {
    $_SESSION["request_details"] = [];
}
$_SESSION["request_details"][] = $requestDetails;
  
// Displaying the total number of 
// requests and request details
echo "Total HTTP Requests: {$_SESSION["request_count"]}<br>";
if ($_SESSION["request_count"] > 1) {
    echo "<h3>Request Details:</h3>";
    echo "<ul>";
    foreach ($_SESSION["request_details"] as $request) {
        echo "<li>{$request["method"]} request from {$request["ip"]}
          at {$request["timestamp"]}</li>";
    }
    echo "</ul>";
}
?>


Output:

Output1

Approach 2: Using File based Counter

In this approach, we will write the PHP script which will store each of the HTTP request count values in the text file named “request_count.txt“. When the server is been requested with the HTTP request, the count value is increased and stored in the text file. So this makes sure that er are getting the updated value from the text file regarding the HTTP requests done on the server.

Example: In the below example, we will find the total number of HTTP requests in PHP using a File-based Counter.

PHP




<?php
/*
 * @param string $filePath The file path to read from.
 * @return int The current request count.
 */
function getReqCnt($filePath)
{
    if (file_exists($filePath)) {
        $reqCnt = intval(file_get_contents($filePath));
    } else {
        $reqCnt = 0;
    }
    return $reqCnt;
}
  
/*
 * @param string $filePath The file path to write to.
 * @param int $count The new request count.
 */
function updateReqCnt($filePath, $count)
{
    file_put_contents($filePath, $count);
}
  
/*
 * @param int $reqCnt The current request count.
 */
function displayRequestCount($reqCnt)
{
    echo "Total HTTP Requests: " . $reqCnt;
}
// Deifning the file path for
// storing the request count
$filePath = "request_count.txt";
try {
    
    // Getting the current request count
    $reqCnt = getReqCnt($filePath);
    
    // Increasing the request count
    $reqCnt++;
    
    // Updating the file with 
    // the new request count
    updateReqCnt($filePath, $reqCnt);
    
    // Displaying the total number of HTTP requests
    displayRequestCount($reqCnt);
} catch (Exception $e) {
    
    // Handling errors
    echo "Error: " . $e->getMessage();
}
?>


Output:

Output2



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads