-2

I made a page which is only accessible for specific ip addresses.

Code:

// IP's that are allowed.
$allow = array("123.456.7.89", "987.654.3.21"); //allowed IPs

if(!in_array($_SERVER['REMOTE_ADDR'], $allow) && !in_array($_SERVER["HTTP_X_FORWARDED_FOR"], $allow)) {

header("Location: http://www.google.com"); //redirect

exit();

}

But if I visit my localhost, I can't visit it because it says my ip is 0.0.0.0

What to fill in as allowed ip?

Julian
  • 33
  • 1
  • 5

2 Answers2

0

Check if you're getting a valid IP Address.

Try this:

$ip = !empty($_SERVER['HTTP_CLIENT_IP']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];

var_dump($ip); // Check if IP is valid

$allow = array("123.456.7.89", "987.654.3.21"); //allowed IPs

if (!in_array($ip, $allow)) {
    header("Location: http://www.google.com"); //redirect
    exit();
}

Reference: How to get the client IP address in PHP?

Community
  • 1
  • 1
Indrasis Datta
  • 8,692
  • 2
  • 14
  • 32
  • What do I need to enter than as allowed ip, if I visit on localhost? Because if I visit it on my mobile phone I am getting a valid ip, and it works if I put that in the allowed ip's. But I am getting 0.0.0.0 for my pc, which doesn't works. – Julian Oct 21 '16 at 11:21
0

Try visiting your localhost on another device, and use that ip.

Julian
  • 361
  • 1
  • 4
  • 18