title: PHP Port Scanning
body: <p>This may be a duplicate post, BUT I've not seen any answer that correctly resolves this.</p>
  
  <p>I'm trying to find a php script that can correctly determine the state of a TCP or UDP port.</p>
  
  <p>I've tried several of the ones I've found online and they all return the wrong results.
  EG: On my local network I have ports 5000 UDP &amp; 5060 TCP/UDP open and routing correctly.</p>
  
  <p>If I run a test via <a href="http://www.ipfingerprints.com/portscan.php" rel="nofollow">http://www.ipfingerprints.com/portscan.php</a> or GRC Shields Up then the correct results are returned,  but all of the PHP Scripts I've tried fail and show the ports as closed.</p>
  
  <p>I'm running the php script via my hosted account and trying to scan and test my own network.  </p>
  
  <p>This is one of the scripts I've tried:</p>
  
  <pre><code>    &lt;html&gt;
  &lt;head&gt;
  &lt;?php echo "&lt;title&gt;Port Scanning " . $_GET['host'] . "&lt;/title&gt;"; ?&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;?php
  ini_set('display_errors', 0);
  
  if(isset($_GET['host']) == true) 
      $host = $_GET['host'];
  else
  {
      echo "You didn't set the host parameter";
      die();
  }
  
  if(isset($_GET['sport']) == true) 
      $sport = $_GET['sport'];
  else
  {
      echo "You didn't set the sport parameter";
      die();
  }
  
  if(isset($_GET['eport']) == true) 
      $eport = $_GET['eport'];
  else
  {
      echo "You didn't set the eport parameter";
      die();
  }
  
  if($sport &gt; $eport)
  {
      $sport = $eport;
  }
  
  $scanned = 0;
  $openports = 0;
  $maxports = 1;
  $totalports = ($eport - $sport) + 1;
  $mins = floor(($totalports / 10) / 60);
  $secs = ($totalports / 10) - $mins * 60;
  
  echo "&lt;font color=\"blue\"&gt;Scanning " . $totalports . " ports on " . $host . ", this should take around " . $mins . " minute(s) and " . $secs . " second(s), probably more depending on local website load, hardware, and other factors.&lt;br/&gt;&lt;br/&gt;&lt;/font&gt;";
  flush();
  
  do
  {
      if($scanned &gt;= $maxports &amp;&amp; $maxports != 0)
      {
          echo "&lt;font color=\"darkgreen\" size=\"4\"&gt;Scan limit of " . $maxports . " ports reached, scanning stopped.&lt;/font&gt;&lt;br/&gt;&lt;br/&gt;&lt;font color=\"darkred\" size=\"4\"&gt;Scanner written by Samo502&lt;/font&gt;";
          flush();
          die();
      }
      if(fsockopen($host, $sport, $errorno, $errorstr, 0.1))
      {
          echo "&lt;font color=\"darkgreen\"&gt;Port " . $sport . " is open on " . $host . "&lt;/font&gt;&lt;br/&gt;";
          $openports++;
          flush();
      }
      else
      {
          echo "&lt;font color=\"red\"&gt;Port " . $sport . " is not open on " . $host . "&lt;/font&gt;&lt;br/&gt;";
          flush();
      }
      $scanned++;
      $sport++;
  } while($sport &lt;= $eport);
  
  echo "&lt;br/&gt;&lt;font color=\"blue\"&gt;Done, " . $openports . " out of " . $totalports . " ports are open.&lt;/font&gt;&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;font color=\"darkred\" size=\"4\"&gt;Scanner written by Samo502&lt;/font&gt;";
  die();
  ?&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Does anyone know of a way to do this correctly ?</p>
  
  <p>Thanks</p>
  
  <p><strong>**UPDATE**</strong>
  I've found this which seems to work a bit better, but it's still not detecting that port 21 is currently open on my network..</p>
  
  <p><a href="http://resources.infosecinstitute.com/php-build-your-own-mini-port-scanner-2/" rel="nofollow">http://resources.infosecinstitute.com/php-build-your-own-mini-port-scanner-2/</a></p>
  
  <p>Any help gratefully received. Thanks</p>