What are the potential pitfalls of using the ping function in PHP to check server status without specifying a port?
When using the ping function in PHP without specifying a port, the default port used is 80. This can lead to inaccurate results if the server is running on a different port. To ensure accurate server status checks, it is important to specify the correct port in the ping function.
$server = 'example.com';
$port = 443; // Specify the correct port here
exec("ping -c 1 $server:$port", $output, $result);
if ($result === 0) {
echo "Server is reachable.";
} else {
echo "Server is unreachable.";
}