Skip to content

Instantly share code, notes, and snippets.

@shahariaazam
Created January 19, 2013 18:38
Show Gist options
  • Save shahariaazam/4574211 to your computer and use it in GitHub Desktop.
Save shahariaazam/4574211 to your computer and use it in GitHub Desktop.
How to Ping to a server to check whether it is up or down.
<?php
/**
* @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";
}
//Echoing it will display the ping if the host is up, if not it'll say "down".
echo PingToServer("www.google.com", 80, 10);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment