Are there any best practices or additional settings that need to be considered when using fsockopen() in PHP to check website reachability?

When using fsockopen() in PHP to check website reachability, it's important to set a timeout value to prevent the script from hanging indefinitely if the website is unreachable. Additionally, error handling should be implemented to catch any potential errors that may occur during the connection attempt.

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

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

if (!$socket) {
    echo "Error: $errstr ($errno)";
} else {
    echo "Website is reachable";
    fclose($socket);
}