chito Posted February 2, 2010 Share Posted February 2, 2010 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/>"; } Quote Link to comment Share on other sites More sharing options...
Mchl Posted February 2, 2010 Share Posted February 2, 2010 try preg_match('#202\.\d{1,3}\.\d{1,3}.\d{1,3}#',$ip) Quote Link to comment Share on other sites More sharing options...
chito Posted February 2, 2010 Author Share Posted February 2, 2010 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(); } Quote Link to comment Share on other sites More sharing options...
thebadbad Posted February 2, 2010 Share Posted February 2, 2010 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)) { //... } Quote Link to comment Share on other sites More sharing options...
chito Posted February 2, 2010 Author Share Posted February 2, 2010 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. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted February 3, 2010 Share Posted February 3, 2010 $ip = ip2long($_SERVER['REMOTE_ADDR']); if (ip2long('202.0.0.0') >= $ip && ip2long('203.0.0.0') < $ip) { // blocked } http://php.net/ip2long Quote Link to comment Share on other sites More sharing options...
Mchl Posted February 3, 2010 Share Posted February 3, 2010 $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... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.