IP filtering for developers

  1. // ------------------------------------------------------------------------
  2. // define these in your main index.php or a constants config file
  3. define('DEVELOPERS', '127.0.0.1|123.123.123.123|111.222.333');
  4. define('REDIRECT_PUBLIC', 'http://en.gravatar.com/<whatever>');
  5.  
  6.  
  7. // ------------------------------------------------------------------------
  8. // developersOnly
  9. // ipDepth allows you to be less restrictive and accept an entire work subnet
  10.  
  11. if ( ! function_exists('developersOnly'))
  12. {
  13.     function developersOnly($ipDepth = 4, $boolResult = false)
  14.     {
  15.         $ip = explode('.', $_SERVER['REMOTE_ADDR']);
  16.         $check_ip = array();
  17.         for($i = 0; $i < $ipDepth; $i++)
  18.             $check_ip[] = $ip[$i];
  19.         $check_ip = implode(".", $check_ip);
  20.  
  21.         if(strpos(DEVELOPERS, $check_ip) === false):
  22.             if($boolResult)
  23.                 return false;
  24.             else
  25.                 redirect(REDIRECT_PUBLIC);
  26.             exit;
  27.         endif;
  28.  
  29.         return true;
  30.     }
  31. }
  32.  
  33.  
  34. // ------------------------------------------------------------------------
  35. // usage examples
  36. // developersOnly();                            // require IP match and redirect if not developer
  37. // developersOnly(3);                           // require IP subnet match and redirect if not developer
  38. // develoeprsOnly(4, true);             // return a boolean result instead of redirect

連絡先: info@paste.jp
Created by Paste.jp - v7.0