Caution: This post was published in 2015 and may contain information, techniques, or code examples that are no longer current. Please double-check official documentation and modern best practices before using anything from this article.

Block access based on Client IP using PHP

Block access based on Client IP

The opposite requires a simple exclamation mark before in_array() to change the script so that only those listed in the array will be allowed to see the site. Anything below this code will only be accessible IPs is in the array.

$valid_ips = array('123.123.123.123','32.23.13.31','123.45.678.90');
if (in_array($_SERVER['REMOTE_ADDR'],$valid_ips)) {
   echo "You do have permission to see this text";
   die();
}

Grant access based on Client IP

This is a useful script to add at the start of any PHP file which you want hidden from a specific IP address or addresses. Anything below this first script will be hidden from anyone listed in the $valid_ips array.

$valid_ips = array('123.123.123.123','32.23.13.31','123.45.678.90');
if (!in_array($_SERVER['REMOTE_ADDR'],$valid_ips)) {
   echo "You do not have permission to see this text";
   die();
}

Ready to elevate your website?

Whether you're launching a new site, strengthening security, or adding a new feature, I can help transform your vision into a high-performing online presence.

Discuss your web development project