PHP

Find Open TCP Port

admin by @admin ADMIN
Jun 16, 2026
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Probe whether a remote host:port accepts connections, with a configurable timeout. Useful for quick health checks in deploy scripts.
PHP
Raw
<?php
function isPortOpen(string $host, int $port, float $timeoutSec = 2.0): bool {
    $fp = @fsockopen($host, $port, $errno, $errstr, $timeoutSec);
    if ($fp) { fclose($fp); return true; }
    return false;
}

var_dump(isPortOpen('google.com', 443));     // true
var_dump(isPortOpen('google.com', 12345));   // false
var_dump(isPortOpen('localhost', 3306));     // true if MySQL is running locally
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.