Check if Network Port is Open using PHP

fsockopen function

<?php

function isNetworkPortOpen(string $hostname, int $port): bool
{
    $conn = @fsockopen($hostname, $port);
    if (false === $conn) {
        return false;
    }

    fclose($conn);

    return true;
}

$isOpen = isNetworkPortOpen('google.com', 80);
echo $isOpen ? '1' : 0; // 1

$isOpen = isNetworkPortOpen('127.0.0.1', 443);
echo $isOpen ? '1' : 0; // 0

Leave a Comment

Cancel reply

Your email address will not be published.