title: Check if an IP address is private
body: <p>I like to check if an IP address is in a private network. It doesn't work. </p>
  
  <p><strong>My code:</strong></p>
  
  <pre><code>&lt;?php
  $ip = $_SERVER['REMOTE_ADDR'];
  
  function _isPrivate($ip) 
  {
      $i = explode('.', $ip);
  
      if ($i[0] == 10) {
          return true;
      } else if ($i[0] == 172 &amp;&amp; $i[1] &gt; 15 &amp;&amp; $i[1] &lt; 32) {
          return true;
      } else if ($i[0] == 192 &amp;&amp; $i[1] == 168) {
          return true;
      }
      return false;
  }
  ?&gt;
  </code></pre>
  
  <p>The other one:</p>
  
  <pre><code>&lt;?php
  $ip = $_SERVER['REMOTE_ADDR'];
  
  function _isPrivate($ip) 
  {
      $ip = ip2long($ip);
      $net_a = ip2long('10.255.255.255') &gt;&gt; 24; 
      $net_b = ip2long('172.31.255.255') &gt;&gt; 20; 
      $net_c = ip2long('192.168.255.255') &gt;&gt; 16; 
  
      return $ip &gt;&gt; 24 === $net_a || $ip &gt;&gt; 20 === $net_b || $ip &gt;&gt; 16 === $net_c; 
  }
  ?&gt;
  </code></pre>
  
  <p>Any help would be much appreciated, thanks!</p>