Page 1 of 1
Blocking IPs that exist in txt file
Posted: Sun Sep 18, 2011 12:44 am
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.
Re: Blocking IPs that exist in txt file
Posted: Sun Sep 18, 2011 1:35 am
by twinedev
Do you have the list of IP's one per line in the file?
-Greg
Re: Blocking IPs that exist in txt file
Posted: Sun Sep 18, 2011 2:09 am
by implications
Yep, I do.
Re: Blocking IPs that exist in txt file
Posted: Sun Sep 18, 2011 3:44 am
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
Re: Blocking IPs that exist in txt file
Posted: Sun Sep 18, 2011 5:49 am
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.
Re: Blocking IPs that exist in txt file
Posted: Sun Sep 18, 2011 7:42 am
by Celauran
Have you checked the $blacklist array?
Re: Blocking IPs that exist in txt file
Posted: Sun Sep 18, 2011 10:07 am
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?
Re: Blocking IPs that exist in txt file
Posted: Sun Sep 18, 2011 10:44 am
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?