Forum Moderators: coopster

Message Too Old, No Replies

allow access by ip(s)

         

RogueDogg

7:14 pm on Mar 3, 2008 (gmt 0)

10+ Year Member



Ok what I am trying to do is allow access by specified IP addresses, here is what I have so far. I can get it to work with setting 1 IP address but to allow multiple IP addresses is where I'm failing.


<?PHP
$userip = ($_SERVER['REMOTE_ADDR']);
$ip = '111.111.111.111';

if ($userip != $ip) {
header("Location:http://".$_SERVER['HTTP_HOST']."/path/to/file_restrict.php");
} else{
header("Location:http://".$_SERVER['HTTP_HOST']."/path/to/file_allow.php");
}
?>

This works, how would I go about specifying a 2nd or 3rd IP address to allow. I've tried using split function and I've also tried to specify like $ip2 or $ip3 and that didn't work, or I couldn't get it to work. Here is the attempts at adding other addresses to allow.


<?PHP
$userip = ($_SERVER['REMOTE_ADDR']);
$ip = '111.111.111.111';
$ip2 = '222.222.222.222';

if ($userip != $ip ¦¦ $ip2) {
header("Location:http://".$_SERVER['HTTP_HOST']."/path/to/file_restrict.php");
} else{
header("Location:http://".$_SERVER['HTTP_HOST']."/path/to/file_allow.php");
}
?>

OR


<?PHP
$userip = ($_SERVER['REMOTE_ADDR']);
$ip = split("111.111.111.111","222.222.222.222","333.333.333.333");

if ($userip != $ip) {
header("Location:http://".$_SERVER['HTTP_HOST']."/path/to/file_restrict.php");
} else{
header("Location:http://".$_SERVER['HTTP_HOST']."/path/to/file_allow.php");
}
?>

....HELP :-)

Thanks in advance

cameraman

7:25 pm on Mar 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if ($userip != $ip ¦¦ $ip2) {

Although this makes perfect sense to me and I don't understand why languages don't implement it that way, that's not quite the syntax you need.

if (($userip != $ip) && ($userip != $ip2)) {

You could also place the allowed ips in an array and use in_array() [us.php.net]

Vis3R

7:29 pm on Mar 3, 2008 (gmt 0)

10+ Year Member



if ($userip != $ip && $ip2) is a valid sintax but does not do what you wanted.. it means if $userip is not $ip and $ip2 is not 0 (false)

[edited by: Vis3R at 7:29 pm (utc) on Mar. 3, 2008]

RogueDogg

7:44 pm on Mar 3, 2008 (gmt 0)

10+ Year Member




if ($userip != $ip ¦¦ $ip2) {
Although this makes perfect sense to me and I don't understand why languages don't implement it that way, that's not quite the syntax you need.

if (($userip != $ip) && ($userip != $ip2)) {

You could also place the allowed ips in an array and use in_array()

Ok that is working, thank you very much. I will look into the in_array() that you suggested.

Greatly appreciated :-)

RogueDogg

7:48 pm on Mar 3, 2008 (gmt 0)

10+ Year Member



Actually before I go...one more quick question possibly not releated to this is, what if I wanted to use dyndns.org because my IP address keeps changing, I've tried replacing $ip = '111.111.111.111'; to $ip = 'account.dyndns.org'; however that doesn't work probabaly cause it doesn't reslove to the actual ip. Any ideas how to make that work ?

Thanks,

cameraman

9:04 pm on Mar 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Since you wouldn't want to allow just anyone from dyndns.org's address range, about the only way is to see what address you have and put that in.

You might want to look in to using logins and sessions or cookies to do what you want. There's been several threads here on the subject, so a search should net you more info than you need to go that route.

RogueDogg

9:57 pm on Mar 3, 2008 (gmt 0)

10+ Year Member



Ok this is what I came up with. Actually what this is doing is sending them to the "login" page if they're on the approved IP address list. So this is what I came up with.


<?PHP
$userip = ($_SERVER['REMOTE_ADDR']);
$ip1 = '111.111.111.111';
$ip2 = gethostbyname('accountname.dyndns.org');

if (($userip != $ip1) && ($userip != $ip2)) {
header("Location:http://".$_SERVER['HTTP_HOST']."/path/to/file_restrict.php");
} else{
header("Location:http://".$_SERVER['HTTP_HOST']."/path/to/file_allow.php");
}
?>

It works, however I never thought about it allowing anyone from dyndns.org but I guess if it does they still need to enter login credentials I am just trying to limit the amount of users getting to that page to begin with. One other thing is if they're not coming from "accountname.dyndns.org" than the IP address wouldn't match and then block them so...yeah this should work perfectly.

Thanks for all your help. :-)

Yeppers Vis3R exactly....

[edited by: RogueDogg at 9:59 pm (utc) on Mar. 3, 2008]

Vis3R

9:57 pm on Mar 3, 2008 (gmt 0)

10+ Year Member



you already solved it yourself while i was typing this msg :)

[edited by: Vis3R at 9:58 pm (utc) on Mar. 3, 2008]