Checking if a Port is Open with PHP

Here’s a simple PHP script to check if a port is open on a specific URL or IP address:


function check_port($host, $port) {
  $connection = @fsockopen($host, $port);
  if (is_resource($connection)) {
    return true;
  } else {
    return false;

$host = '';
$port = 80;

if (check_port($host, $port)) {
  echo "$host:$port is open\n";
} else {
  echo "$host:$port is closed\n";


You can replace with the desired URL or IP address and change the port number to the desired port. When you run the script, it will return “open” if the port is open and “closed” if the port is closed.


Leave a Reply

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