Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit execution of a php-page to requests made from a certain ip-range

I have a PHP page I need to limit execution access of to only clients inside our firewall.

How would I write a php-script that can look up the clients ip-address and match it to a ip-range (for instance 10...* or 200.10.10.*).

like image 598
Johan Carlsson Avatar asked Dec 29 '22 20:12

Johan Carlsson


1 Answers

You can use ip2long to convert dotted quads to long values, then just perform some arithmetic to check a given network/mask combination:

$network=ip2long("200.10.10.0");
$mask=ip2long("255.255.255.0");

$remote=ip2long($_SERVER['REMOTE_ADDR']);

if (($remote & $mask) == $network)
{
   //match!
}
else
{
   //does not match!
}
like image 108
Paul Dixon Avatar answered Jan 13 '23 20:01

Paul Dixon