Skip to content
limit by ip address in php

Limit Access to Designated IP Addresse

PHP Code Snippets > Limit Access to Designated IP Addresse

by Steve Dawson Web Developer

This PHP script will only allow access to the webpage/website if the IP number is allowed by the script. You can enter an unlimited number of IP numbers.
This is a great way to only allow access to those who you want to have access.

Use and Installation

Create a file and call it ip-block.php then copy and paste the following code into this file. You can add the IP Addresses to the allowed_ips ARRAY in the script. This will only allow those listed to access the page.

// enter the IP numbers here, who you want to have access
$allowed_ips = array(

// set the loops to 0 before we start the check 
$a 0;
$i 0;
// start checking the ip numbers to see if they are allowed
while ($allowed_ips[$i+1] !== null)
if (
preg_match($allowed_ips[$i], $_SERVER['REMOTE_ADDR']))
$a=$a+1; }

// if ip address is not found send them somewhere else ...
if ($a == 0)   {
 echo (
"Sorry, you do not have access to this section, please <a href=\"\">click here</a>");

Now you can call the file as an include and add it to all the pages you want to allow access for. Add the code below to the top of each page you want to allow access for.

<?php include ("ip-block.php"); ?>

Free Quote available for any custom PHP and Database Software, Contact Steve Dawson Today