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 8240547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:31:25+00:00 2026-06-07T20:31:25+00:00

this has been bugging me for ages now but i can’t figure it out..

  • 0

this has been bugging me for ages now but i can’t figure it out..

Basically i’m using a hit counter which stores unique IP address in a file. But what i’m trying to do is get it to count how many hits each IP address has made.

So instead of the file reading:

222.111.111.111  
222.111.111.112  
222.111.111.113  

I want it to read:

222.111.111.111 - 5  
222.111.111.112 - 9  
222.111.111.113 - 41  

This is the code i’m using:

    $file = "stats.php";  
    $ip_list = file($file);  
    $visitors = count($ip_list);  

    if (!in_array($_SERVER['REMOTE_ADDR'] . "\n", $ip_list))  
    {  
    $fp = fopen($file,"a");  
    fwrite($fp, $_SERVER['REMOTE_ADDR'] . "\n");  
    fclose($fp);  
    $visitors++;  
    }

What i was trying to do is change it to:

    if (!in_array($_SERVER['REMOTE_ADDR'] . " - [ANY NUMBER] \n", $ip_list))  
    {  
    $fp = fopen($file,"a");  
    fwrite($fp, $_SERVER['REMOTE_ADDR'] . " - 1 \n");  
    fclose($fp);  
    $visitors++;  
    }

    else if (in_array($_SERVER['REMOTE_ADDR'] . " - [ANY NUMBER] \n", $ip_list))  
    {  
    CHANGE [ANY NUMBER] TO [ANY NUMBER]+1
    }

I think i can figure out the last adding part, but how do i represent the [ANY NUMBER] part so that it finds the IP whatever the following number is?

I realise i’m probably going about this all wrong but if someone could give me a clue i’d really appreciate it.

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-07T20:31:27+00:00Added an answer on June 7, 2026 at 8:31 pm

    What you are doing is a very bad idea

    But lets first answer the actual question you are asking.

    To be able to do that you will have to actually process the file first in some kind of data structure that allows for that to be done. I’d presonally recommend an array in the form of IP => AMOUNT.

    For example (untested code):

    $fd = file($file);
    $ip_list = array();
    for ($fd as $line) {
       list($ip, $amount) = explode("-", $line);
       $ip_list[$ip] = $amount;
    }
    

    Note that the code is not perfect as it would leave a space at the end of $ip and another in front of $amount due to the nature of your original data. But it works good enough just to point you in the right direction. A more “accurate” solution would involve regular expressions or modifying the original data source to a more convenient format.


    Now the real answer to your actual problem

    Your process will quickly become a performance bottleneck as you would have to open up that file, process it and write it all back again afterwards (not sure if you can do in-line editing of an open file) for every request.

    As you are trying to do some kind of per-IP hit count, there are a lot of better solutions to your problem:

    • Use an existing solution for it (like piwik)
    • Use an actual database for your data
    • Keep your file simple with just a list of IPs and post-process it off-line periodically to make it be the format you want
    • You can avoid writing that file altogether if you have access to your webserver’s logs (and they are setup to log every request with the originating IP) and you can post-process that file instead
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This has been bugging me all morning and I can't even figure out where
This question has been bugging me for a long time now but essentially I'm
This one has been bugging me for a while now, but I never seem
This has been bugging me for the last few hours now. I am using
This has been bugging me for a long time now and I can't seem
This has been bugging me for more than two days now, so i thought
So this has been bugging me for some time and I can't seem to
It's not a programming questions. But this question has been bugging me for awhile.
This Jquery problem has been bugging me for a while now. I developed a
This issue has been on/off bugging me and I've written 4 wrappers now. I'm

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.