Redirect users to another URL by IP using PHP

Here’s a handy snippet I’ve used a few times now. I find it handy for quick redirects should you want to send all other users to another page such as a maintenance page, coming soon page or other holding page. It allows all users NOT using the IP listed to be sent elsewhere, allowing those with matching IPs to view the site as normal.

To use, simply add this to the top of your header or index file.

if (!in_array(@$_SERVER['REMOTE_ADDR'], array('127.0.0.1', '123.456.789.123')))
{
header('Location: http://www.example.com/sitemaintenance.php');
exit;
}

Add your allowed IP’s in the array above, and the location of where you want non-matching IPs to be sent (eg a coming soon/maintenance page)

Useful post? Share it

Leave a Reply

Your email address will not be published. Required fields are marked *