In daily development, we sometimes need to check whether a specific port of a remote server is open, such as checking whether a web service (such as 80, 443), a database service (such as 3306, 5432), or a custom port is running normally. fsocckopen() is a native function provided by PHP that can be used to establish socket connections, which also allows us to use it for simple port probing.
This article will introduce how to use the fsocckopen() function to detect whether the remote server port is open, and provide specific implementation steps and precautions.
fsocckopen() is a function used to open a network connection or Unix socket connection. The syntax is as follows:
resource|false fsockopen(
string $hostname,
int $port,
int &$error_code = null,
string &$error_message = null,
float $timeout = ini_get("default_socket_timeout")
)
Parameter description:
$hostname : The target host, which can be a domain name or IP address.
$port : The port number to connect to.
$error_code (optional): Error code when connection fails.
$error_message (optional): Error message when connection fails.
$timeout (optional): Connection timeout (seconds).
We try to connect to the port of the specified address through fsocckopen() . If the connection is successful, we can think that the port is open; otherwise, the port is considered to be closed or the host is unreachable.
<?php
function isPortOpen($host, $port, $timeout = 5)
{
$errno = 0;
$errstr = '';
$connection = @fsockopen($host, $port, $errno, $errstr, $timeout);
if (is_resource($connection)) {
fclose($connection);
return true;
} else {
return false;
}
}
// Example:Test gitbox.net of 443 Is the port open?
$host = 'gitbox.net';
$port = 443;
if (isPortOpen($host, $port)) {
echo "port {$port} exist {$host} 上是开放of。";
} else {
echo "port {$port} exist {$host} Not open or unreachable。";
}
Define the target host and port number <br> Determine the server address and port you want to detect, such as gitbox.net and 443 .
Trying to connect using fsocckopen() <br> Use @fsocopen() to suppress the default error output and determine whether the connection is successful by the return value.
Processing connection results
Success: means the port is open;
Failed: Indicates that the port is not open, which may be due to the server firewall or the port is not listening.
Close the connection resource <br> If the connection is successful, you should use fclose() to close the connection resource to avoid resource leakage.
In the website monitoring system, periodically check whether the service port is normal;
In the automated deployment tool, confirm that the target service is ready;
Quickly detect port accessibility during local development and debugging;
Integrate port detection function in operation and maintenance tool scripts.
fsocopen() is not suitable for high concurrency port detection scenarios, and its efficiency is relatively low;
In some server configurations, this function may be prohibited due to security policies;
When using @ to suppress error output, make sure you handle exceptions well;
The detected server may be configured with IP whitelists, firewalls, etc., which will affect the detection results.