Member Avatar for JayGeePee

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):

<?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.
::1
100:100:1000
123:234:345

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

The function is never called. When it's called, it only echoes something if the IP is not found.

Member Avatar for JayGeePee

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.

<?php

include 'count.php';

hit_count();

?>

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';
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.