1

I have an area on my site that I would like to only give access to a few people. My code now only works with one ip address, but I would like to be able to add more.

Here is what I am using:

$ipaddress = $_SERVER['REMOTE_ADDR'];
if($ipaddress == '111.111.111.111') {
//Action for allowed IP Addresses
} else {
//Action for all other IP Addresses
echo 'You are not authorized here.'; 
echo "<br />IP Address: ".$_SERVER['REMOTE_ADDR'];
exit;
}
sarsar
  • 1,431
  • 4
  • 14
  • 17

1 Answers1

12
$whitelist = array('111.111.111.111', '111.111.111.112');
if (in_array($_SERVER['REMOTE_ADDR'], $whitelist)) {
    //Action for allowed IP Addresses
} else {
    //Action for all other IP Addresses
    echo 'You are not authorized here.'; 
    echo "<br />IP Address: ".$_SERVER['REMOTE_ADDR'];
    exit;
}
zerkms
  • 249,484
  • 69
  • 436
  • 539
  • @zerkms thank you works perfectly. i will accept your answer when the limit is up :) – sarsar Sep 19 '11 at 00:13
  • Is this the best way to check this? What if the remote user is behind a proxy? I suppose the proxy would have to be on the white list, making it a moot point. – MrOodles Jan 07 '13 at 15:00
  • 2
    @MrOodles: not the best, but the only. There is no data you can trust more than `$_SERVER['REMOTE_ADDR']` (assuming your webserver is configured to initialize it with the client ip) – zerkms Jan 07 '13 at 19:58