Code snippet: Restrict access to a page in PHP based on remote hosts IP’s

<?php 

//get the remote host IP
$szRemoteIP = $_SERVER['REMOTE_ADDR'];

//this is the array holding the allowed IP's list;
$arrAllowedIPs = array("192.168.0.1",
                       "192.168.2.3",
                       "192.168.63.22",
                       "127.0.0.1",
                       "10.10.192.102");

if (!in_array($szRemoteIP, $arrAllowedIPs))
{
    //if IP's is not in the list, deny access
    echo "You don't have the permissions to access this area from $szRemoteIP";
    exit(0);
}

//page code goes here
// ...

?>

VN:F [1.9.11_1134]
Rating: 8.6/10 (16 votes cast)
Code snippet: Restrict access to a page in PHP based on remote hosts IP's, 8.6 out of 10 based on 16 ratings

About Armino

This website is about me, my experiences, my hobbies and my friends. If you are interested in my professional profile you can check it on LinkedIn.