1

I currently have a working script that counts the number of views and stores them in a .txt file.

It's working fine but how do I make it so that it limits to your IP Address?

I tried this but it's not counting.

// Get filename of Page
$pageName = basename($_SERVER["SCRIPT_FILENAME"], '.php');
$ip = $_SERVER['REMOTE_ADDR']?:($_SERVER['HTTP_X_FORWARDED_FOR']?:$_SERVER['HTTP_CLIENT_IP']);

// Remove .php extension
$counterName = basename($pageName, ".php").".txt";

// Open the file for reading
// "a+" Read & write the file. Create file if not exist.
$fp = fopen($counterName, "a+");
$fpIP = fopen("ip_".$counterName, "a+");

fwrite($fpIP, $ip."-");

// Get the existing count
$count = fread($fp, 1024);

// Close the file
fclose($fp);

// Add 1 to the existing count
$count = $count + 1;

// Reopen the file and erase the contents
$fp = fopen($counterName, "w");

$ipRead = file_get_contents('ip_index.txt');

if(strpos($ipRead, "$ip") !== FALSE) {
     echo $count;
}
else {
    fwrite($fp1, $count);
    echo $count;    
}
fclose($fp);

Below is my updated code with Barmar's code (fully working) which will show each individual visitor how many times they have been to your page based on their IP address.

// Get filename of Page
$pageName = basename($_SERVER["SCRIPT_FILENAME"], '.php');

// Remove .php extension
$counterName = basename($pageName, ".php").".counter";

// Get IP
$ip = $_SERVER['REMOTE_ADDR']?:($_SERVER['HTTP_X_FORWARDED_FOR']?:$_SERVER['HTTP_CLIENT_IP']);


$count_text = @file_get_contents($counterName);
$counters = $count_text ? json_decode($count_text, true) : array();
if (isset($counters[$ip])) {
    $counters[$ip]++;
} else {
    $counters[$ip] = 1;
}
file_put_contents($counterName, json_encode($counters));
echo $counters[$ip];
Matt
  • 15
  • 4
  • Is there a specific reason you're storing the count in a file? Also if you echo $ip what is the value? – kyle Oct 17 '17 at 00:30
  • You either need to put the IP into the filename, or put an associative array that's keyed by IP into the file. – Barmar Oct 17 '17 at 00:52

1 Answers1

0

Store an associative array that's keyed off $ip in the counter file.

$count_text = @file_get_contents($counterName);
$counters = $count_text ? json_decode($count_text, true) : array();
if (isset($counters[$ip])) {
    $counters[$ip]++;
} else {
    $counters[$ip] = 1;
}
file_put_contents($counterName, json_encode($counters));
echo $counters[$ip];

You don't need the ip_XXX.txt file in this design.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks for your help, I think I'm almost there but I'm getting the following error with my script: Warning: Cannot use a scalar value as an array in ****/test/index.php on line 29 I have updated my code above :) – Matt Oct 20 '17 at 00:02
  • I undid your edit. If you want to show the new code, add it as an addition, don't remove the original code that the question was about. – Barmar Oct 20 '17 at 00:05
  • Did you empty the original counter file? My code expects the file to contain a JSON array or be empty, in which case it initialize it with an empty array. if it still contains the number from your version, it won't work. – Barmar Oct 20 '17 at 00:07
  • Doh sorry my bad! Yes I deleted it now and it counted 1 the first time, then the 2nd time it came up with this error "Fatal error: Cannot use object of type stdClass as array in /home/******/test/index.php on line 26. Then when I view the counter file it says "{"my.ip.addr.ess":1}" – Matt Oct 20 '17 at 00:09
  • Sorry, forgot to use the second argument to `json_decode()`. – Barmar Oct 20 '17 at 00:11
  • You didn't add the edit that I just made: `json_decode($count_text, true)` – Barmar Oct 20 '17 at 00:14
  • Looks like I posted it after I saw this, thanks for ur patience with me! It's not giving any errors anymore, but it's counting up like before I used your code, any ideas? Thanks allot. – Matt Oct 20 '17 at 00:17
  • Are you sure it's getting called from different IPs? Do you see those IPs in the file? – Barmar Oct 20 '17 at 00:20
  • Oh wow I see what your script does, it shows each unique user how many times they have viewed the page themselves. This is really cool, I will definitely use this for another purpose. I was initially wanting a counter which only counts up if your IP has not been to the page before, to assist with giving slightly more accurate page view results to everyone, and also add a time so that if they return tomorrow it WILL count up, but if they return in say 30 mins, it WONT count. – Matt Oct 20 '17 at 00:27
  • Oh, you're trying to get a count of unique visitors. Instead of putting a per-IP count in the file, put the last visit time in the file. When they come back, get the difference between the current time and the last time, if it's higher than your time threshold increment the counter. Either way, update the per-IP timestamp. – Barmar Oct 20 '17 at 00:34
  • Thanks allot I'll try that later! – Matt Oct 20 '17 at 00:42