0

I'm trying to create a script to store IP adresses in a text file and than to count unique IP. For example if i have 3 different IP in the txt file

111.111.111.111
111.111.111.111
111.111.111.111
222.222.222.222
222.222.222.222
000.000.000.000
000.000.000.000
000.000.000.000
000.000.000.000
000.000.000.000

Than this is the result that i'm trying to have

111.111.111.111 = 3 (var = $total_visits)
222.222.222.222 = 2 (var = $total_visits)
000.000.000.000 = 5 (var = $total_visits)

I know that would be more realistic to built a database, but this function is for a special page that has a small number of users, let say 20-30 unique visitor per week.

My final goal is to warning the users like that:

$x = 5; // total visits of a unique IP;
if ($x > 6)
{
header('Location: http://example.com/banned.php');
}
elseif ($x > 5)
{
echo 'First Warn! Go away!';
}
7
  • 2
    What code have you developed so far? This is not a programming service. Commented Aug 23, 2015 at 23:15
  • Why does it matter what my answer is? Your arrogance take it for yourself Commented Aug 23, 2015 at 23:17
  • 2
    how much big the file is? how many lines? hint: secure.php.net/manual/en/function.array-count-values.php Commented Aug 23, 2015 at 23:19
  • It doesnt work getting from the text file. This is what i did: $log = 'log.txt'; $ip = $_SERVER['REMOTE_ADDR']; $file = file_get_contents($log); $fp = fopen($log, 'a+'); fwrite($fp, '"' . $ip. '", ' . "\r\n"); fclose($fp); echo $file . '<br>'; $lines = count(file($log)); Commented Aug 23, 2015 at 23:21
  • the problem is that the code is counting the lines. Im trying to count the unique ip, not all IPs Commented Aug 23, 2015 at 23:25

1 Answer 1

1

I guess what you want is

print_r(array_count_values(explode("\r\n", $file)));

since $file is the data you read from the file. As long as the file is small enough.

Sign up to request clarification or add additional context in comments.

3 Comments

Finally, thank you. Ps: I came here to learn, not to teach. Nobody is born learned
Of course not, but I could not have answered that as short and easy without you posting your file-code. No offense or arrogance meant. ;)
Well, as i said the code in the post was for the FINAL GOAL. But i said also that the result that i'm trying to have is to group unique ip in one value. Maybe i wasnt clearly, but your code did the job. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.