Skip to content

Instantly share code, notes, and snippets.

@rpfilomeno
Last active August 29, 2015 14:10
Show Gist options
  • Save rpfilomeno/440085d956f7d62ec3ec to your computer and use it in GitHub Desktop.
Save rpfilomeno/440085d956f7d62ec3ec to your computer and use it in GitHub Desktop.
PHP Code Snippets
function ping($host, $timeout = 1) {
/* ICMP ping packet with a pre-calculated checksum */
$package = "\x08\x00\x7d\x4b\x00\x00\x00\x00PingHost";
$socket = socket_create(AF_INET, SOCK_RAW, 1);
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $timeout, 'usec' => 0));
socket_connect($socket, $host, null);
$ts = microtime(true);
socket_send($socket, $package, strLen($package), 0);
if (socket_read($socket, 255)) {
$result = microtime(true) - $ts;
} else {
$result = false;
}
socket_close($socket);
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment