Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jul 26th, 2009  |  syntax: None  |  size: 4.86 KB  |  views: 195  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.     <?php
  2.    
  3.     function checkATT() {
  4.         $ipaddy = $_SERVER['REMOTE_ADDR'];
  5.         /*echo $ipaddy;
  6.         echo ip_in_range($ipaddy, '12.20.0.0/8');
  7.         echo ip_in_range($ipaddy, '76.192.0.0/10');
  8.         echo ip_in_range($ipaddy, '68.88.0.0/13');
  9.         echo ip_in_range($ipaddy, '75.0.0.0/10');
  10.         echo ip_in_range($ipaddy, '99.128.0.0/10'); */
  11.         if (ip_in_range($ipaddy, '209.0.0.0/10') ||ip_in_range($ipaddy, '99.128.0.0/10') || ip_in_range($ipaddy, '75.0.0.0/10') || ip_in_range($ipaddy, '68.88.0.0/13') ||  ip_in_range($ipaddy, '76.192.0.0/10') || ip_in_range($ipaddy, '12.20.0.0/8')) {
  12.             echo 'Link to informative article or redirect to same.';
  13.         } else {
  14.             echo '<!-- Probably nothing here, to not bother non-ATT customers. -->';
  15.         }
  16.        
  17.     }
  18.    
  19.     /*
  20.      * ip_in_range.php - Function to determine if an IP is located in a
  21.      *                   specific range as specified via several alternative
  22.      *                   formats.
  23.      *
  24.      * Network ranges can be specified as:
  25.      * 1. Wildcard format:     1.2.3.*
  26.      * 2. CIDR format:         1.2.3/24  OR  1.2.3.4/255.255.255.0
  27.      * 3. Start-End IP format: 1.2.3.0-1.2.3.255
  28.      *
  29.      * Return value BOOLEAN : ip_in_range($ip, $range);
  30.      *
  31.      * Copyright 2008: Paul Gregg <pgregg@pgregg.com>
  32.      * 10 January 2008
  33.      * Version: 1.2
  34.      *
  35.      * Source website: http://www.pgregg.com/projects/php/ip_in_range/
  36.      * Version 1.2
  37.      *
  38.      * This software is Donationware - if you feel you have benefited from
  39.      * the use of this tool then please consider a donation. The value of
  40.      * which is entirely left up to your discretion.
  41.      * http://www.pgregg.com/donate/
  42.      *
  43.      * Please do not remove this header, or source attibution from this file.
  44.      */
  45.    
  46.    
  47.     // decbin32
  48.     // In order to simplify working with IP addresses (in binary) and their
  49.     // netmasks, it is easier to ensure that the binary strings are padded
  50.     // with zeros out to 32 characters - IP addresses are 32 bit numbers
  51.     Function decbin32 ($dec) {
  52.       return str_pad(decbin($dec), 32, '0', STR_PAD_LEFT);
  53.     }
  54.    
  55.     // ip_in_range
  56.     // This function takes 2 arguments, an IP address and a "range" in several
  57.     // different formats.
  58.     // Network ranges can be specified as:
  59.     // 1. Wildcard format:     1.2.3.*
  60.     // 2. CIDR format:         1.2.3/24  OR  1.2.3.4/255.255.255.0
  61.     // 3. Start-End IP format: 1.2.3.0-1.2.3.255
  62.     // The function will return true if the supplied IP is within the range.
  63.     // Note little validation is done on the range inputs - it expects you to
  64.     // use one of the above 3 formats.
  65.     Function ip_in_range($ip, $range) {
  66.       if (strpos($range, '/') !== false) {
  67.         // $range is in IP/NETMASK format
  68.         list($range, $netmask) = explode('/', $range, 2);
  69.         if (strpos($netmask, '.') !== false) {
  70.           // $netmask is a 255.255.0.0 format
  71.           $netmask = str_replace('*', '0', $netmask);
  72.           $netmask_dec = ip2long($netmask);
  73.           return ( (ip2long($ip) & $netmask_dec) == (ip2long($range) & $netmask_dec) );
  74.         } else {
  75.           // $netmask is a CIDR size block
  76.           // fix the range argument
  77.           $x = explode('.', $range);
  78.           while(count($x)<4) $x[] = '0';
  79.           list($a,$b,$c,$d) = $x;
  80.           $range = sprintf("%u.%u.%u.%u", empty($a)?'0':$a, empty($b)?'0':$b,empty($c)?'0':$c,empty($d)?'0':$d);
  81.           $range_dec = ip2long($range);
  82.           $ip_dec = ip2long($ip);
  83.    
  84.           # Strategy 1 - Create the netmask with 'netmask' 1s and then fill it to 32 with 0s
  85.           #$netmask_dec = bindec(str_pad('', $netmask, '1') . str_pad('', 32-$netmask, '0'));
  86.    
  87.           # Strategy 2 - Use math to create it
  88.           $wildcard_dec = pow(2, (32-$netmask)) - 1;
  89.           $netmask_dec = ~ $wildcard_dec;
  90.    
  91.           return (($ip_dec & $netmask_dec) == ($range_dec & $netmask_dec));
  92.         }
  93.       } else {
  94.         // range might be 255.255.*.* or 1.2.3.0-1.2.3.255
  95.         if (strpos($range, '*') !==false) { // a.b.*.* format
  96.           // Just convert to A-B format by setting * to 0 for A and 255 for B
  97.           $lower = str_replace('*', '0', $range);
  98.           $upper = str_replace('*', '255', $range);
  99.           $range = "$lower-$upper";
  100.         }
  101.    
  102.         if (strpos($range, '-')!==false) { // A-B format
  103.           list($lower, $upper) = explode('-', $range, 2);
  104.           $lower_dec = (float)sprintf("%u",ip2long($lower));
  105.           $upper_dec = (float)sprintf("%u",ip2long($upper));
  106.           $ip_dec = (float)sprintf("%u",ip2long($ip));
  107.           return ( ($ip_dec>=$lower_dec) && ($ip_dec<=$upper_dec) );
  108.         }
  109.    
  110.         echo 'Range argument is not in 1.2.3.4/24 or 1.2.3.4/255.255.255.0 format';
  111.         return false;
  112.       }
  113.    
  114.     }
  115.    
  116.     checkATT();
clone this paste RAW Paste Data