Blocking IPs that exist in txt file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
implications
Forum Commoner
Posts: 25
Joined: Thu Apr 07, 2011 3:59 am

Blocking IPs that exist in txt file

Post by implications »

I have a txt file stored with IPs and I want to block IPs that exist in that txt file. I currently have it going on like this:

Code: Select all

$blacklist = file("blacklist.txt");

if (in_array ($_SERVER['REMOTE_ADDR'], $blacklist)) {
	exit("IP has been blocked from accessing this page.");
It works if the blacklist variable is an array but I can't seem to get the txt file to be read as an array.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Blocking IPs that exist in txt file

Post by twinedev »

Do you have the list of IP's one per line in the file?

-Greg
implications
Forum Commoner
Posts: 25
Joined: Thu Apr 07, 2011 3:59 am

Re: Blocking IPs that exist in txt file

Post by implications »

Yep, I do.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Blocking IPs that exist in txt file

Post by twinedev »

Well, not sure then, as file reads in each line as a element in an array, so the only thing I can think of is wrong file type for defining line breaks. can be a difference between UNIX/DOS/MAC format (ie, unix is \n (newline) DOS is actually \r\n (carriage return/newline). Not sure what a MAC uses, but that could be it.

Try making a temp file from PHP:

Code: Select all

$fp = fopen('blacklist.txt','w');
frwrite($fp,"10.0.0.1\n");
frwrite($fp,"130.101.206.49\n");
frwrite($fp,"192.168.39.1\n");
fclose($fp);
If you create the file with that, and then it works I would say it is definitely an issue with how the file was created.

-Greg
implications
Forum Commoner
Posts: 25
Joined: Thu Apr 07, 2011 3:59 am

Re: Blocking IPs that exist in txt file

Post by implications »

I've tried it but it doesn't seem to work either. I don't think it has to do with the way the file was created but the way it's read by PHP. The contents of the text file is broken up into lines without whitespace so there shouldn't be an issue.

It seems to work when there's only one IP address in the .txt file, however. If I put my own IP address in and try submitting something through the form, the script terminates and displays the appropriate error. But if I add some additional random IP addresses beneath my own and try submitting the form again, my IP has no longer been banned and I'm able to post through the form.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Blocking IPs that exist in txt file

Post by Celauran »

Have you checked the $blacklist array?
implications
Forum Commoner
Posts: 25
Joined: Thu Apr 07, 2011 3:59 am

Re: Blocking IPs that exist in txt file

Post by implications »

I've finally fixed it by reading the .txt file using fopen and fread then spitting it out as an array. Maybe it's because the txt file can't be read as a valid array?

But I have another question regarding the whole .txt file thing. Does anyone know how I can do something that allows me to check whether the IP already exists in the .txt file so if it already exists, PHP throws an error?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Blocking IPs that exist in txt file

Post by Celauran »

You'd probably need to read the text file into an array then check if the IP in question exists in that array. Any reason you're not using SQL for this?
Post Reply