sarsar
sarsar

Reputation: 1561

white list ip address for admin access

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

Upvotes: 1

Views: 8005

Answers (1)

zerkms
zerkms

Reputation: 254916

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

Upvotes: 12

Related Questions