Alright I'll try to be as clear as possible. I'm currently doing tutorials online because I'm learning php. I'm stuck on an issue I'm not really sure of. I'm currently coding on my local machine and think it may have something to do with that, but I'm not sure. Here it is.

This currently involves 2 files(count.php and ip.txt)

Here's the first script(count.php):


function hit_count() {
  $ip_address = $_SERVER['REMOTE_ADDR'];

         $ip_file = file('ip.txt');
         foreach ($ip_file as $ip) {
                 $ip_single = trim($ip);
                 if ($ip_address!=$ip_single) {
                   $found = false;
                 } else {
                   $found = true;
         if ($found==false) {
          echo 'IP not found.';


And the second one is simply(ip.txt):

All random numbers I came up with for testing purposes.

I cannot get this display in the browser. I'm not sure if it's my local machine, the coding, etc.

4 Years
Discussion Span
Last Post by paulkd

How simple was that.. lol! It's obvious I need to find better tutorials, because this code is to the T copied from a tutorial. I'm confused because as it sits, it's working for the fella who put the tut up on youtube.

All I did was created an index.php file and did this.


include 'count.php';



But this isn't the way he was doing it.


If you don't need the trim (you know that the txt file records are already trimmed) you could simply use an array function.

$ip_needle = $_SERVER['REMOTE_ADDR'];
$ip_haystack = file('/ip.txt',FILE_IGNORE_NEW_LINES);
echo in_array($ip_needle,$ip_haystack) ? $ip_needle.' found' : $ip_needle.' not found';
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.