Avel
Avel

Reputation: 111

Php: count values from a text file

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!';
}

Upvotes: 0

Views: 715

Answers (1)

PhilMasteG
PhilMasteG

Reputation: 3185

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.

Upvotes: 1

Related Questions