What are common issues when using fsockopen for server status checks in PHP scripts?

One common issue when using fsockopen for server status checks in PHP scripts is that it can sometimes result in slow or unresponsive checks due to network latency or server timeouts. To solve this, you can set a timeout value for the fsockopen function to limit the amount of time it waits for a response from the server.

$host = 'example.com';
$port = 80;
$timeout = 5; // Timeout in seconds

$socket = fsockopen($host, $port, $errno, $errstr, $timeout);

if (!$socket) {
    echo "Server is unreachable or timeout occurred.";
} else {
    echo "Server is reachable.";
    fclose($socket);
}