HSKrustofsky Posted February 20, 2009 Share Posted February 20, 2009 This is the script I am working with, and everything is working fine... <?php $ip_list = array('123.456.789.10'); $ip_range = array('123.456.789.*'); $user_ip = $_SERVER['REMOTE_ADDR']; if(in_array($user_ip, $ip_list)) { header("Location:http://".$_SERVER['HTTP_HOST']."/enterpage.html"); } if(!empty($ip_range)) { foreach($ip_range as $range) { $range = str_replace('*','(.*)', $range); if(preg_match('/'.$range.'/', $user_ip)) { header("Location:http://".$_SERVER['HTTP_HOST']."/enterpage.html"); } } } ?> I just recent got a set of IPs where they want a specific range. For example: 123.456.789.1-55. Where they want to allow access to 1-55, but deny 56-255. What I was wondering is, is there an easy/fast way to do this without having to type all the numbers(123.456.789.1, 123.456.789.2,123.456.789.3, etc), 'til I get to 55? Thank you in advanced for your help. Link to comment https://forums.phpfreaks.com/topic/146059-solved-ip-range-help/ Share on other sites More sharing options...
JoeBuntu Posted February 20, 2009 Share Posted February 20, 2009 if(ereg("123\.456\.789\.[1-55]", $ip_range)) true would be a ip within your range. where $ip_range would be a string Link to comment https://forums.phpfreaks.com/topic/146059-solved-ip-range-help/#findComment-766756 Share on other sites More sharing options...
farkewie Posted February 20, 2009 Share Posted February 20, 2009 <?php $ip = "123.123.123.45"; $check = explode('.', $ip); if( $check[3] < 50){ // range allowed } ?> prob a better way but thats what i know :-) Link to comment https://forums.phpfreaks.com/topic/146059-solved-ip-range-help/#findComment-766757 Share on other sites More sharing options...
JoeBuntu Posted February 20, 2009 Share Posted February 20, 2009 I take mine back, that does not work correctly- sorry. explode is probably the way to do it Link to comment https://forums.phpfreaks.com/topic/146059-solved-ip-range-help/#findComment-766762 Share on other sites More sharing options...
HSKrustofsky Posted February 20, 2009 Author Share Posted February 20, 2009 Thank you very much, farkewie. That worked perfectly well. I just have 1, well 2 more question. First, what if I want to do this for multiple addresses? Will I have to imput the code multiple times? I have no probelem with that. Also, I was wondering what if it was a range of ranges? For example: 123.456.7-22.*. Is there such a thing? What I was thinking, is not adding the *, and just having that range within the third set of numbers. Will that work properly? Once again, thank you very much for your help. Link to comment https://forums.phpfreaks.com/topic/146059-solved-ip-range-help/#findComment-767266 Share on other sites More sharing options...
HSKrustofsky Posted February 21, 2009 Author Share Posted February 21, 2009 Like I said earlier I got it to work, but for some weird reason it only works with one. I am trying to add multiple IPs, and it won't let me. Here is the code I am trying to work with... $ip = "123.123.75"; $check = explode('.', $ip); if( $check[0] == 123 && $check[1] == 123 && $check[2] >= 34 && $check[2] <= 75) { header("Location:http://".$_SERVER['HTTP_HOST']."/enter.html"); } $ip2 = "123.123.123.190"; $check2 = explode('.', $ip2); if( $check2[0] == 123 && $check2[1] == 123 && $check2[2] == 123 && $check2[3] >= 1 && $check2[3] <=190) { header("Location:http://".$_SERVER['HTTP_HOST']."/enter.html"); } Well what is happening is, instead of only allowing these IP ranges to enter.html, it is allowing everyone, and I don't want that. Any suggestions? Link to comment https://forums.phpfreaks.com/topic/146059-solved-ip-range-help/#findComment-767828 Share on other sites More sharing options...
Cal Posted February 21, 2009 Share Posted February 21, 2009 the variable $ip actualy needs to be the visitors IP address, otherwise the comparisons are useless if the ip is set to one that passes it all. $ip = $_SERVER['REMOTE_ADDR']; Link to comment https://forums.phpfreaks.com/topic/146059-solved-ip-range-help/#findComment-767833 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.