Posted By

nkm on 07/06/10


Tagged

check local address home localhost detector


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Vordreller


Is Local Address


 / Published in: PHP
 

Checks if a given address is local. Useful for setting up the dev environment automatically.

NOTE: It's not intended for validation!

  1. function is_local($str)
  2. {
  3. return (bool) preg_match('/^local\.|\.local$|^localhost$|^127\.0\.0\.1$|^::1$|^10\.|^169\.254\.|^172\.(?:1[6-9]|2[0-9]|[3[0-1]])\.|^192\.168\./', $str);
  4. }
  5.  
  6. // Examples:
  7.  
  8. var_dump(is_local('snipplr.com')); // returns bool(false)
  9. var_dump(is_local('local.snipplr.com')); // returns bool(true)
  10. var_dump(is_local('snipplr.local')); // returns bool(true)
  11. var_dump(is_local('localhost')); // returns bool(true)
  12. var_dump(is_local('127.0.0.1')); // returns bool(true)
  13. var_dump(is_local('::1')); // returns bool(true)
  14. var_dump(is_local('10.0.1.35')); // returns bool(true)
  15. var_dump(is_local('169.254.23.42')); // returns bool(true)
  16. var_dump(is_local('172.24.1.3')); // returns bool(true)
  17. var_dump(is_local('172.15.16.23')); // returns bool(false)
  18. var_dump(is_local('172.42.23.16')); // returns bool(false)
  19. var_dump(is_local('10.0.1.35')); // returns bool(true)
  20. var_dump(is_local('192.168.1.4')); // returns bool(true)

Report this snippet  

You need to login to post a comment.