Jump to content

Output By IP Range


xProteuSx

Recommended Posts

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

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.