"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.
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?
Something along these lines:
PHP Code:
$firstthreenums = substr ( $ip , 0 , 11 )
$lastnum = substr ( $ip , 12 , 3 )
$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.
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 02:29 AM .
Thanks
Hey thanks everyone for your help and your fast response!
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!
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
Forum Rules
Bookmarks