Ping Function PHP


SUBMITTED BY: Guest

DATE: July 5, 2013, 2:01 p.m.

FORMAT: PHP

SIZE: 821 Bytes

HITS: 822

  1. <?PHP
  2. function ping($host, $timeout = 1) {
  3. /* ICMP ping packet with a pre-calculated checksum */
  4. $package = "\x08\x00\x7d\x4b\x00\x00\x00\x00PingHost";
  5. $socket = socket_create(AF_INET, SOCK_RAW, 1);
  6. socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $timeout, 'usec' => 0));
  7. socket_connect($socket, $host, null);
  8. $ts = microtime(true);
  9. socket_send($socket, $package, strLen($package), 0);
  10. if (socket_read($socket, 255))
  11. $result = microtime(true) - $ts;
  12. else $result = false;
  13. socket_close($socket);
  14. return $result;
  15. }
  16. ?>

comments powered by Disqus