I am trying to make a blocklist for my site, and I've got a text file with several IPs seperated by a newline. I would like to use a function that returns either TRUE if the string (the IP) is found in the file, or FALSE if it is not.
Thanks!
Looking for a function to find a string in a file
Moderator: General Moderators
-
Daisy Cutter
- Forum Commoner
- Posts: 75
- Joined: Sun Aug 01, 2004 9:51 am
- trukfixer
- Forum Contributor
- Posts: 174
- Joined: Fri May 21, 2004 3:14 pm
- Location: Miami, Florida, USA
Code: Select all
$target = $_SERVER['REMOTE_ADDR'];
$string = file_get_contents('/var/www/whatever/your_ip_file.txt');
$iparr = explode('\n',$string);
if(in_array($target,$ip_arr));
{
$test = true;
}
else
{
$test = false;
}Yay for the PHP Manual. The besterest resource on the net!
-
Daisy Cutter
- Forum Commoner
- Posts: 75
- Joined: Sun Aug 01, 2004 9:51 am
Thanks a lot! That does it's job well.trukfixer wrote:Code: Select all
$target = $_SERVER['REMOTE_ADDR']; $string = file_get_contents('/var/www/whatever/your_ip_file.txt'); $iparr = explode('\n',$string); if(in_array($target,$ip_arr)); { $test = true; } else { $test = false; }
I guess I was looking for the wrong thing, I was in the PHP manual looking for preg_match and stuff.
Code: Select all
$string = file_get_contents('/var/www/whatever/your_ip_file.txt');
$iparr = explode('\n',$string);