xProteuSx Posted March 24, 2008 Share Posted March 24, 2008 I am writing a small snippet which displays a particular HTML table but only if the IP is a Canadian one. I have downloaded a file with Canadian IP ranges from http://www.ipaddresslocation.org/ip_ranges/get_ranges.php and I am trying to figure out what the easiest way to run the script is, without causing the PHP server to crap out. I don't know how hard it is for a server to run an IF statement that includes so many 'variables'. Any ideas? Would it be best to do this as an array? If so, how do I go about doing it? Thanks in advance guys! Quote Link to comment https://forums.phpfreaks.com/topic/97672-output-by-ip-range/ Share on other sites More sharing options...
Caesar Posted March 24, 2008 Share Posted March 24, 2008 What do the IP ranges look like (Eg. 72.87.*.*)? Might be able to provide some help with example code if I had an idea of what they were. If anything, give you some ideas. :-) Quote Link to comment https://forums.phpfreaks.com/topic/97672-output-by-ip-range/#findComment-499765 Share on other sites More sharing options...
xProteuSx Posted March 24, 2008 Author Share Posted March 24, 2008 Here is a sample section of the file: 24.108.0.0 24.109.255.255 24.114.0.0 24.114.255.255 24.122.0.0 24.122.255.255 24.129.224.0 24.129.239.255 24.137.32.0 24.137.47.255 24.137.64.0 24.137.127.255 24.137.192.0 24.137.223.255 24.138.0.0 24.138.191.255 24.139.0.0 24.139.31.255 24.141.0.0 24.141.255.255 24.146.0.0 24.146.31.255 24.150.0.0 24.150.255.255 24.153.0.0 24.153.31.255 24.156.128.0 24.156.159.255 24.200.0.0 24.203.255.255 24.207.0.0 24.207.127.255 24.212.0.0 24.212.127.255 24.213.64.0 24.213.95.255 24.215.0.0 24.215.127.255 24.222.0.0 24.222.255.255 24.224.128.0 24.224.255.255 It goes on and on, but it looks like this ... hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/97672-output-by-ip-range/#findComment-499768 Share on other sites More sharing options...
ohdang888 Posted March 24, 2008 Share Posted March 24, 2008 well they all start with first 2 numbers as 24. try this.... $data = $ip address of the person; if (preg_match('/(\d+)./', $data, $first_two_numbers)) { echo "The first to numbers are ".$first_two_numbers[1]; }else{ echo "no working"; } Quote Link to comment https://forums.phpfreaks.com/topic/97672-output-by-ip-range/#findComment-499782 Share on other sites More sharing options...
xProteuSx Posted March 24, 2008 Author Share Posted March 24, 2008 I understand what you are saying, but the problem is that the IP's do not start with only 24. They start with a whole whack of different numbers. I could write a huge series of IF statments, but that would be very harsh on the server, right? I mean, it would have a branching effect and would result in hundreds, if not thousands, of IF statements. I'm wondering if there isn't a way to load the numbers into an array and just run it through a single loop. Know what I mean? Quote Link to comment https://forums.phpfreaks.com/topic/97672-output-by-ip-range/#findComment-499790 Share on other sites More sharing options...
Caesar Posted March 24, 2008 Share Posted March 24, 2008 Or I suppose you can take the reverse approach & compare the user's IP to the IP's in your file.... <?php $iprange = file_get_contents('ca_ips.txt'); $userip = $_SERVER['REMOTE_ADDR']; $iparray = explode(".",$userip); if(preg_match('/'.$iparray[0].'\.'.$iparray[1].'\.[0-9]+\.[0-9]+/', $iprange)) { //Do your thang here } ?> Should work. Quote Link to comment https://forums.phpfreaks.com/topic/97672-output-by-ip-range/#findComment-499792 Share on other sites More sharing options...
xProteuSx Posted March 24, 2008 Author Share Posted March 24, 2008 Caesar, thanks for you input. I will try this out soon, and I will get back to you about whether it works. Thanks a bunch. Quote Link to comment https://forums.phpfreaks.com/topic/97672-output-by-ip-range/#findComment-499801 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.