1

How to check if the client is on intra-net or inter-net?

I tried this:

function isLocal(){
        $ip=$_SERVER["REMOTE_ADDR"];
        return $ip=="127.0.0.1"||startsWith($ip,"192.168.")||startsWith($ip,"10.")||startsWith($ip,"172.16.");
    }

But is this enought? I think no, moreover this fails with ipV6 address, is there a smarter way?

Tobia
  • 9,165
  • 28
  • 114
  • 219
  • Did you search about CIDR masks to compare IP addresses to a sub-network? See http://stackoverflow.com/questions/594112/matching-an-ip-to-a-cidr-mask-in-php5 – Ambroise Maupate Jan 31 '15 at 11:37
  • The "172.16." won't get all the Class B PrivateUse addresses since its mask is not an octet boundary. Also, you are missing several local address ranges: CarrierNAT, LinkLocal, TestNet1, Benchmark, TestNet2, TestNet3. The local unicast list is: 10.0.0.0/8, 100.64.0.0/10, 127.0.0.0/8, 169.254.0.0/16, 172.16.0.0/12, 192.0.2.0/24, 192.168.0.0/24, 198.18.0.0/15, 198.51.100.0/24, 203.0.113.0/24. There are also local broadcast and multicast ranges. – Ron Maupin Jan 31 '15 at 16:20
  • Thanks, seems to be a little difficult to manage it... – Tobia Feb 02 '15 at 08:00

0 Answers0