crjunk crjunk - 2 months ago 9
PHP Question

Redirect User if Internal IP Address

I am not familiar with PHP but I need to create a simple page to temporarily redirect internal users until a production problem is fixed.

If the user's IP address starts with

"10."
,
"192."
, or
"172."
then I need to redirect them to another server. If the user's IP address does not meet this criteria then I need to display a message telling the user the site is down for maintenance.

Can someone help me with this?

Answer
$chunks = explode('.', $_SERVER['REMOTE_ADDR']);
$whitelist = array(10, 192, 172);
$server = "http://example.com";
if(in_array($chunks[0], $whitelist))
{
    //redirect to another server
    header("Location: " . $server);
    die();
}
else
{
    //Show maintenance message
    die("The site is down for maintenance.");
}
Comments