How To Make A Port Scanner in PHP

Port scanners are created to find opened/closed ports in a host, you can get the same results in this Port Scanner coded in php by the way I wouldn't take all the credits to this because i am a php beginner so I had to make this from scratch also there are many more open source advanced Port Scanners. You can also easily add Ports if you need to.


Codes : 
<html>
<head>
<title>Port Scanner</title>
</head>
<form method="post" >
    Domain name/IP: 
    <input type="text" class = text name="domain" /> 
    <input type="submit" class="s" value="Scan" />
</form>
<style type="text/css">
.text {
font-weight:400;
color:#000;
letter-spacing:1pt;
word-spacing:2pt;
font-size:12px;
text-align:left;
font-family:arial, helvetica, sans-serif;
line-height:1;
}

.s {
-moz-box-shadow:inset 0 1px 0 0 #9acc85;
-webkit-box-shadow:inset 0 1px 0 0 #9acc85;
box-shadow:inset 0 1px 0 0 #9acc85;
background:linear-gradient(tobottom,#74ad5a5%,#68a54b100%);
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#74ad5a',endColorstr='#68a54b',GradientType=0);
background-color:#74ad5a;
border:1px solid #3b6e22;
display:inline-block;
color:#fff;
font-family:arial;
font-size:13px;
font-weight:700;
text-decoration:none;
padding:6px 12px;
}

.s:hover {
background:linear-gradient(tobottom,#68a54b5%,#74ad5a100%);
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#68a54b',endColorstr='#74ad5a',GradientType=0);
background-color:#68a54b;
}

.s:active {
position:relative;
top:1px;
}
<br />
<?php
if(!empty($_POST['domain'])) {    
    //ports
    $ports = array(20, 21, 22, 23, 25, 53, 443, 143, 80, 109, 110, 1433, 1434, 156, 994);
    
    $results = array();
    foreach($ports as $port) {
        if($pwn = @fsockopen($_POST['domain'], $port, $str, $str_string, 1)) {
            $results[$port] = true;
            fclose($pwn);
        } else {
            $results[$port] = false;
        }
    }
 
    foreach($results as $port=>$val)    {
        $port = getservbyport($port,"tcp");
                echo "Port $port ($port): ";
        if($val) {
            echo "<span style=\"color:green\">Opened</span><br/>";
        }
        else {
            echo "<span style=\"color:red\">Closed</span><br/>";
        }
    }
}
?>
</html>
Usage : 
Enter domain or ip address and click on the button then it would take around a minute and would return back with the ports & it's status.

This is what I got for my Domain : 
  1. Port ftp-data (ftp-data): Closed
  2. Port ftp (ftp): Closed
  3. Port ssh (ssh): Closed
  4. Port telnet (telnet): Closed
  5. Port smtp (smtp): Opened
  6. Port domain (domain): Closed
  7. Port https (https): Opened
  8. Port imap (imap): Opened
  9. Port http (http): Opened
  10. Port pop2 (pop2): Closed
  11. Port pop3 (pop3): Opened
  12. Port ms-sql-s (ms-sql-s): Closed
  13. Port ms-sql-m (ms-sql-m): Closed
  14. Port sqlsrv (sqlsrv): Closed
  15. Port ircs (ircs): Closed
Referrance
Enjoy the port scanner..