A Simple Port Scanner in PHP

July 17, 2012 | By

Here is a simple port scanner written in PHP. It checks if a port is accessible by trying to connect to it using PHP’s fsockopen function.

The first foreach loop goes through the port numbers in the $ports array trying to connect to each one using the fsockopen function (PHP.net reference.) The ampersand (@) is to suppress warnings when the port is inaccessible. The result of each attempt is stored in the $results array. The second loop just goes through the list of results to display it to the user. Hope you found this useful.

Check out the Host Services tool to see the code in action. Hope you found this post useful.

Filed in: PHP | Tags:

Comments (4)

  1. on line 25:

    echo “Port $port (“. $prot . “): “;

    $port*

  2. Nice script, thank you..

  3. mto bom, obrigado m)

  4. Alexander

    Thank you! Very big!