www.webdeveloper.com
Results 1 to 7 of 7

Thread: "If" Statement thats Targets Multiple Ranges

  1. #1
    Join Date
    Sep 2004
    Posts
    114

    "If" Statement thats Targets Multiple Ranges

    In PHP I have a line of code similar to:

    PHP Code:
    $ip $REMOTE_ADDR ;

    if (
    $ip == "137.229.183.43"

            
    header("Location:http://www.site-a.com");
        }
    else 
        { 
            
    header("Location:http://www.site-b.com");
        } 
    This code works great for me. It allows me to have a certain IP address be redirect to one site, while other IP addresses get directed elsewhere.

    My question is, how would I work it so that instead of targeting one address, I would target a range? Such as:

    137.229.183.30 137.229.183.240

    Also, using the same if statement line, how could I target multiple ranges? Such as:

    137.229.183.30 137.229.183.240 & 31.41.59.26 31.41.59.250

    Thank you very much for any help.

  2. #2
    Join Date
    Jul 2003
    Location
    New York City
    Posts
    2,771
    Hmmm, interesting. Well, IPs aren't strictly numerical. What one could do, is eliminate the . and make it numerical, and then use range. Like:

    PHP Code:
    <?PHP
    $ip 
    str_replace('.'''$_SERVER['REMOTE_ADDR']);

    if ((
    $ip>=13722918330&&$ip<=137229183240)||($ip>=31415926&&$ip<=314159250))
    {
            
    header("Location:http://www.site-a.com"); 
        }
    else
        {
            
    header("Location:http://www.site-b.com");
        }
    ?>
    Here, we take the period out of the IP and make it basically a numerical string. Here, we're glad that PHP, unlike other computer languages, doesn't require us to delcare variable types. So, now, we check to see that it's in the range of the two IP addresses we desire. Note, that we must remove the periods in the two IP's for the range to make this work. I used parentheses and the || to create two ranges. Is this similar to what you need?

  3. #3
    Join Date
    Aug 2004
    Posts
    142
    Something along these lines:

    PHP Code:
    $firstthreenums substr($ip011)
    $lastnum substr($ip123)

    $permissions = array(
    array(
    'xxx.xxx.xxx','lll','hhh'),
    array(
    'xxx.xxx.xxx','lll','hhh'),
    array(
    'xxx.xxx.xxx','lll','hhh'),
    array(
    'xxx.xxx.xxx','lll','hhh')); 
    Then just have a loop that runs through the outer array, checks for a match with $firstthreenums and then checks the presence of $lastnum in the range specified by the other elements.

    Yeah, there's probably a really elegant way to do this with associative arrays, but this would work fine.

  4. #4
    Join Date
    Mar 2004
    Location
    London, UK
    Posts
    342
    You really don't need anything elaborate.

    PHP will make alphanumeric comparisons as though they were numeric.

    In other words, "John" is less than "Peter"...dictionary order.
    So there's no need to remove the period in the IP address.

    Check out the following...

    PHP Code:

    <form method="post" action="<?php echo $_SERVER['PHP_SELF'?>">
    Enter IP Address: 
    <input type="text" name="ip" />
    <input type="submit" name="submit" value="Submit" />

    </form>


    <?php
    if($_POST['ip'])
    {
        
    $ip trim($_POST['ip']);
        
        if((
    $ip >= "111.112.113.114") && ($ip <= "444.445.446.447"))
        {
            echo 
    "$ip is within the acceptable range";
        }
        else
        {
            echo 
    "$ip is outside the acceptable range";
        }
    }


    ?>
    Last edited by solavar; 11-05-2004 at 03:29 AM.

  5. #5
    Join Date
    Sep 2004
    Posts
    114

    Thanks

    Hey thanks everyone for your help and your fast response!

  6. #6
    Join Date
    Jul 2003
    Location
    New York City
    Posts
    2,771
    Originally posted by solavar
    You really don't need anything elaborate.

    PHP will make alphanumeric comparisons as though they were numeric.

    In other words, "John" is less than "Peter"...dictionary order.
    So there's no need to remove the period in the IP address.

    Check out the following...

    PHP Code:

    <form method="post" action="<?php echo $_SERVER['PHP_SELF'?>">
    Enter IP Address: 
    <input type="text" name="ip" />
    <input type="submit" name="submit" value="Submit" />

    </form>


    <?php
    if($_POST['ip'])
    {
        
    $ip trim($_POST['ip']);
        
        if((
    $ip >= "111.112.113.114") && ($ip <= "444.445.446.447"))
        {
            echo 
    "$ip is within the acceptable range";
        }
        else
        {
            echo 
    "$ip is outside the acceptable range";
        }
    }


    ?>
    Really? Huh, I didn't realize that. I was thinking that both had to be strictly numerical. Aye, thanks as well, solavar!

  7. #7
    Join Date
    Mar 2004
    Location
    London, UK
    Posts
    342
    Thanks, MstrBob



    solavar

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles