What potential pitfalls should be considered when using fsockopen in PHP to check server availability?
One potential pitfall when using fsockopen in PHP to check server availability is that it may lead to resource exhaustion if not handled properly. To mitigate this, it's important to close the socket connection after checking the server availability to release the resources.
$host = 'example.com';
$port = 80;
$timeout = 5;
$socket = fsockopen($host, $port, $errno, $errstr, $timeout);
if ($socket) {
// Server is available
fclose($socket);
} else {
// Server is not available
}