Advertisement
Guest User

Untitled

a guest
Feb 10th, 2013
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. file iplist.txt:
  2. 192.168.0.0/24
  3. 172.16.0.0/16
  4. 10.0.0.0/8
  5.  
  6. <?php
  7. ########### ncritten's function myip2long
  8.  
  9. function myip2long($ip) {
  10. if (is_numeric($ip)) {
  11. return sprintf("%u", floatval($ip));
  12. } else {
  13. return sprintf("%u", floatval(ip2long($ip)));
  14. }
  15. }
  16.  
  17. ########### function to chek ip if it in one of denyied/allowed networks =)
  18.  
  19. function ipfilter($ip) {
  20. $match = 0;
  21.  
  22. ### converting ip address in binary
  23. $ip_addr = decbin(myip2long($ip));
  24.  
  25. ### the file wich contains allowed/denyied networks
  26. if (fopen("iplist.txt", "r")) {
  27. $source = file("iplist.txt");
  28.  
  29. foreach ($source as $line) {
  30.  
  31. ### exploding each network to obtaid network address and cidr
  32. $network = explode("/", $line);
  33. $net_addr = decbin(myip2long($network[0]));
  34. $cidr = $network[1];
  35.  
  36. ### and finaly cheking quantity of network bits from left to right wich is equal to cidr is equal to the same bits of ip address
  37. if (substr($net_addr, 0, $cidr) == substr($ip_addr, 0, $cidr)) {
  38. $match = 1;
  39. break;
  40. }
  41. }
  42. }
  43. return $match;
  44. }
  45.  
  46. ### this function will return 1 if IP match to some network or 0 if will not match
  47.  
  48. ### and finaly the chek will be like this
  49.  
  50. $user_ip = $_SERVER['REMOTE_ADDR'];
  51.  
  52. if (ipfilter($user_ip) == 1) echo "allowed!";
  53. else echo "deny!";
  54.  
  55. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement