What potential error can occur when using fsockopen() to check website reachability and how can it be handled?
When using fsockopen() to check website reachability, a potential error that can occur is a timeout if the website is unreachable or if the connection takes too long. This can be handled by setting a timeout value for the connection, so that if it exceeds this time, an error can be caught and handled accordingly.
$host = 'www.example.com';
$port = 80;
$timeout = 5; // Set timeout value to 5 seconds
$fp = @fsockopen($host, $port, $errno, $errstr, $timeout);
if (!$fp) {
echo "Error: $errstr ($errno)";
} else {
echo "Website is reachable!";
fclose($fp);
}