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

More Website Development posts

Structured data for SEO

Adding structured data to a website is one of those steps that often gets overlooked, yet it can make a real difference to how a site performs in search engines.…

Continue reading

Ensuring a smooth deployment of a PHP website application

Deploying a PHP website application requires meticulous planning, a solid understanding of the environment, and a thorough execution process. To ensure a smooth deployment, it is essential to follow best…

Continue reading

Knack API Integration

Knack is a powerful low-code platform that allows you to build online databases and custom web applications. When integrating Knack with your own systems or custom front ends, you can…

Continue reading

Popular PHP frameworks

Here’s detailed look at some of the most popular PHP frameworks, each of which can help speed up development, improve code organisation, and enhance application performance. Laravel Laravel is one…

Continue reading