AnrDaemon

Check if an IP is from private subnet

May 22nd, 2017
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.43 KB | None | 0 0
  1. <?php
  2.  
  3. function inet_private($addrString)
  4. {
  5.   $addr = inet_pton($addrString);
  6.   if(strlen($addr) !== 4)
  7.     throw new Exception('Only valid IPv4 addresses are currently supported.');
  8.  
  9.   $ta = unpack('n*', $addr);
  10.   $addr = reset($ta);
  11.   return $addr === 49320 // 192.168.0.0/16
  12.     || ($addr &~ 15) === 44048 // 172.16.0.0/12
  13.     || ($addr &~ 255) === 32512 // 127.0.0.0/8
  14.     || ($addr &~ 255) === 2560; // 10.0.0.0/8
  15. }
Advertisement
Add Comment
Please, Sign In to add comment