Created
January 19, 2013 18:38
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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