What are the potential pitfalls of using fsockopen in PHP to check server availability?

One potential pitfall of using fsockopen in PHP to check server availability is that it can be slow if the server is unresponsive, leading to delays in your script execution. To solve this issue, you can set a timeout value for the fsockopen function to limit the time it waits for a response from the server.

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

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

if($socket){
    echo "Server is available";
    fclose($socket);
} else {
    echo "Server is not available";
}