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

  • SEARCH
  • Home
  • 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 9086439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:29:22+00:00 2026-06-16T21:29:22+00:00

I have written a very simple page counter and a logging script that increments

  • 0

I have written a very simple page counter and a logging script that increments a counter stored in a file and logs information about the client’s operating system and which browser they use. It’s a simple spare time project I’ve been working on, and as such it is extremely rudimentary, writing the counter and the logged information in a designated folder for each page on the site, in a new file for each day.

The thing is, I recently used blitz.io to test my site, and when I ran a “Rush” of 250 requests per second, the counters and the logs were completely flushed, except for the very last query.

I’m not perfectly sure what happened, but I suspect something along the lines of PHP not properly finishing up the previous query before taking on the next one.

I use file_get_contents()/file_put_contents() for the both of them, instead of file(). Would changing to file() solve the problem?

Here’s the counter:

    $filename = '.' . $_SERVER['PHP_SELF'];
$counterpath = '/Websites/inc/logs/counters/total/' . getCurrentFileName() . '-counter.txt';
$globalcounter = '/Websites/inc/logs/counters/total/global-counter.txt';

if (file_exists($counterpath)) {
    $hit_count = file_get_contents($counterpath);
    $hit_count++;
    file_put_contents($counterpath,$hit_count);
}
else {
    $hit_count = "1";
    file_put_contents($counterpath, $hit_count);
}

And here’s the logger:

$logdatefolder = '/Websites/inc/logs/ip/' . date('Y-m-d',$_SERVER['REQUEST_TIME_FLOAT']);
    $logfile = $logdatefolder . "/" . getCurrentFileName() . '-iplog.html';
    $ua = getbrowser();
    if (false == (file_exists($logdatefolder))) {
        mkdir($logdatefolder);
    }
    function checkRef() {
        if (!isset($_SERVER['HTTP_REFERER'])) {
            //If not isset -> set with dummy value
            $_SERVER['HTTP_REFERER'] = 'N/A';
        } 
        return $_SERVER['HTTP_REFERER'];
    }

    /* Main logger */
    $logheader = "<!DOCTYPE html><html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en-US\"><head><title>" . getCurrentFileName() . " log</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /></head><body>";
    $logentry = date("Y-m-d, H:i:s, O T") . ":" . 
    "<br />- Requesting: http://giphtbase.org" . $_SERVER['REQUEST_URI'] . 
    "<br />- Arriving from: " . checkRef() . 
    "<br />- Browser: " . $ua['browser'] . 
    "<br />- Full browser name: " . $ua['name'] . 
    "<br />- Operating system: " . $ua['platform'] . 
    "<br />- Full user agent: " . $ua['userAgent'] . 
    "<br />";
    $logfooter = "<!-- Bottom --></body></html>";

    if (file_exists($logfile)) {
        $logPage = file_get_contents($logfile);
        $logContents = str_replace("<!-- Bottom --></body></html>","",$logPage);
        file_put_contents($logfile, $logContents . $logentry . $logfooter);
    }
    elseif (false == (file_exists($logfile))) {
        file_put_contents($logfile, $logheader . $logentry . $logfooter);
    }
  • 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-16T21:29:23+00:00Added an answer on June 16, 2026 at 9:29 pm

    You should use the FILE_APPEND flag in your file_put_contents() otherwise you will only ever see the last entry:

    file_put_contents($logfile, $logContents . $logentry . $logfooter, FILE_APPEND);
    

    As for the counter, it looks like the file is trying to be written to too many times by different threads, causing it to be inaccessible. You should either use a database, or create a file_lock, or create temporary files and run a cronjob to do the math.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a very simple CTE expression that retrieves a list of all
I have written a very simple WCF service, that worked fine (code below), then
I have written a very very very simple!! script in php. header redirection not
I have a very simple DLL written in unmanaged C++ that I access from
I have written a simple jQuery script that changes the hash tag of a
I have written a very simple Chrome extension. It consists of this background page:
I have written a very simple test application that creates a websocket and parses
I have written a very simple script with jQuery, but it does not work
I have written a very simple bash script to help me migrate from dev
I have written very very simple console application which supports some command line options.

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.