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/>";
    
}

 

Link to comment
Share on other sites

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();
}

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.