How to Ping to a Server in PHP

Sometimes we need to check whether any specific server are up or down. It’s sometimes so much important. So that I have prepared the following code snippet which will tell you about the server whether it is down or up.

I have used fSockOpen in this case to test it. so the function I have created is

 * @param null $host
 * @param null $port
 * @param null $timeout
 * @return bool|string
function PingToServer($host=null, $port=null, $timeout=null)
    if(empty($host) || empty($port) || empty($timeout)){
        return false;
    $TimeStart = microtime(true);
    $Response = fSockOpen($host, $port, $errno, $errstr, $timeout);
    if (!$Response) { return "down"; }
    $TimeEnd = microtime(true);
    return round((($TimeEnd - $TimeStart) * 1000), 0)." ms";

Here is the hostname or server address, 80 is the port number to check and 10 is the timeout period.

Happy coding!!

Shaharia is a professional software engineer with more than 10 years of experience in the relevant fields. Digital ad certified, cloud platform architect, Big data enthusiasts, tech early adopters.

Site Footer