Jump to content

Blocking an IP address - proper formatting of the ereg expression


chito

Recommended Posts

I have a script exercise that is supposed to deny someone with an IP starting 202.xxx.xx.xxx from accessing a download link as well as checking for browser compatibility. What I have works, but then an IP that happened to have "202" anywhere in the address would be blocked, not just one that began with that. I can't seem to find anything describing the proper way to format an expression to do so. Im starting to think the books I have suck or my googleing skills are slipping. Thanks for the help.

 

$useragent = $_SERVER['HTTP_USER_AGENT'];


$remoteaddr = $_SERVER['REMOTE_ADDR'];


$deny =  '202';


if (ereg($deny, $remoteaddr)) {  
    echo "<h3>Sorry you can not download the requested file, please feel free to visit our friends and file your grievence <a href='http://www.fbi.gov/'>Click Here</a></h3>";
    exit();
}

elseif (preg_match('|Windows|', $useragent) && (preg_match('|MSIE|', $useragent))) {
  echo "<h3> Your file is now ready for download,<a href='http://google.com'>Click Here</a> to begin.";
}

elseif (preg_match('|Macintosh|', $useragent) && (preg_match('|Firefox|', $useragent))) {
    echo "<h3> Your file is now ready for download,<a href='http://google.com'>Click Here</a> to begin.";
}

else {
    echo "<h3> Sorry but your browser appears to be incompatible with our downloader</h3><br/>";
    echo "<b> If you are using a Windows computer please download the latest version Internet Explorer <a href='http://www.microsoft.com/nz/windows/internet-explorer/default.aspx'>Here</a></b><br/>";
    echo "<b> If you are using a Macintosh computer please download the latest version of Firefox <a href='http://www.mozilla.com/en-US/firefox/u'>Here</a></b><br/>";
    
}

 

Thanks I gave that a try, it still blocked an ip when it found '202' anywhere in the ip.

I just need to block an ip starting with '202' this seemed to pick it up anywhere in the address.

$useragent = $_SERVER['HTTP_USER_AGENT'];


#$remoteaddr = $_SERVER['REMOTE_ADDR'];
$remoteaddr = '192.202.0.102';


#$deny =  '202';


if (preg_match('#202\.\d{1,3}\.\d{1,3}.\d{1,3}#',$remoteaddr)) {  
    echo "<h3>Sorry you can not download the requested file, please feel free to visit our friends and file your grievence <a href='http://www.fbi.gov/'>Click Here</a></h3>";
    exit();
}

 

The fastest way to check if it begins with 202. is using substr():

 

if (substr($remoteaddr, 0, 4) == '202.') {
//...
}

The regex approach could be

 

if (preg_match('~^202\.~', $remoteaddr)) {
//...
}

 

Yup that did the trick.. Thank you.

$ip = ip2long($_SERVER['REMOTE_ADDR']);
if (ip2long('202.0.0.0') >= $ip && ip2long('203.0.0.0') < $ip) {
    // blocked
}

 

http://php.net/ip2long

 

I was actually trying to come up with some method of using ip2long and binary logic operators but didn't succeed... yet... :P

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.