How can one efficiently check if a server is online using PHP?
To efficiently check if a server is online using PHP, you can use the `fsockopen` function to establish a connection to the server. If the connection is successful, the server is online; otherwise, it is offline.
function isServerOnline($server, $port) {
$connection = @fsockopen($server, $port);
if ($connection){
fclose($connection);
return true;
} else {
return false;
}
}
// Usage
$server = 'example.com';
$port = 80;
if (isServerOnline($server, $port)) {
echo 'Server is online';
} else {
echo 'Server is offline';
}