/    Sign up×
Community /Pin to ProfileBookmark

"If" Statement thats Targets Multiple Ranges

In PHP I have a line of code similar to:

[code=php]$ip = $REMOTE_ADDR ;

if ($ip == “137.229.183.43”)
{
header(“Location:http://www.site-a.com”);
}
else
{
header(“Location:http://www.site-b.com”);
}[/code]

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. ?

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@MstrBobNov 05.2004 — Hmmm, interesting. Well, IPs aren't strictly numerical. What one could do, is eliminate the . and make it numerical, and then use range. Like:

[code=php]
<?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");
}
?>
[/code]


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?
Copy linkTweet thisAlerts:
@mikepurvisNov 05.2004 — Something along these lines:

[code=php]
$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'));
[/code]


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.
Copy linkTweet thisAlerts:
@solavarNov 05.2004 — 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...

[code=php]

<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";
}
}


?>

[/code]
Copy linkTweet thisAlerts:
@shopkanjiauthorNov 05.2004 — Hey thanks everyone for your help and your fast response!
Copy linkTweet thisAlerts:
@MstrBobNov 05.2004 — [i]Originally posted by solavar [/i]

[B]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...



[code=php]

<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";
}
}


?>

[/code]
[/B][/QUOTE]


Really? Huh, I didn't realize that. I was thinking that both had to be strictly numerical. Aye, thanks as well, solavar!
Copy linkTweet thisAlerts:
@solavarNov 05.2004 — Thanks, MstrBob

?

solavar
×

Success!

Help @shopkanji spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...