Normally the port numbers are assigned in various ways its like based on system, user and the dynamic or private.
   Let us see how to find Specific ports opened from the server.

The below code is find the SMTP, POP and IMAP ports is opened in server,

If the port is open displays output is "open" else shows "not responding".



$ports[] = array('host'=>'thelinuxfaq.com','number'=>80);
$ports[] = array('host'=>'smtp.thelinuxfaq.com','number'=>587);
$ports[] = array('host'=>'smtp.thelinuxfaq.com','number'=>465);
$ports[] = array('host'=>'pop.thelinuxfaq.com','number'=>995);
$ports[] = array('host'=>'imap.thelinuxfaq.com','number'=>993);

foreach ($ports as $port)
{

        $connections = @fsockopen($port['host'], $port['number'], $errno, $errstr, 5); // 5 second timeout for each port.

    if (is_resource($connection))
    {
        echo '

' . $port['host'] . ':' . $port['number'] . ' ' . '(' . getservbyport($port, 'tcp') . ') is open.

' . "\n";
        fclose($connection);
    }
    else
    {
        echo '

' . $port['host'] . ':' . $port['number'] . ' is not responding.

' . "\n";
    }
}


Output is,

thelinuxfaq.com:80 () is open.

smtp.thelinuxfaq.com:587 () is not responding.

smtp.thelinuxfaq.com:465 () is not responding.

pop.thelinuxfaq.com:995 () is open.

imap.thelinuxfaq.com:993 () is open.