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:

<?php

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

$host = 'example.com';
$port = 80;

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

?>

You can replace example.com 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.


Comments

Leave a Reply

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