Redirect users to another URL by IP using PHP

By 8th January 2015Blog

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('', '123.456.789.123')))
header('Location: http://www.example.com/sitemaintenance.php');

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)



I'm a Wordpress web developer and website designer based in Cheshire, UK. I specialise in Wordpress, Responsive design and website conversion. | Twitter | Linkedin

Leave a Reply