php check port

$host = 'stackoverflow.com';
$ports = array(21, 25, 80, 81, 110, 443, 3306);

foreach ($ports as $port)
{
$connection = @fsockopen($host, $port);

if (is_resource($connection))
{
echo '

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

' . "\n";

fclose($connection);
}

else
{
echo '

' . $host . ':' . $port . ' is not responding.

' . "\n";
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *