title: PHP - ICMP Ping
body: <p>So I'm trying to work out how to ping a couple of hosts reliably across multiple systems from PHP. For this I've decided the best way is to get PHP to do the ICMP ping so that I'm not using exec or similar.</p>
  
  <p>My current solution (found on google) is as follows:</p>
  
  <pre><code>function testPing($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' =&gt; $timeout, 'usec' =&gt; 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===false?2:0); // 0 = good, 2 = bad
  }
  </code></pre>
  
  <p>which currently results in <code>Warning: socket_create(): Unable to create socket [10013]: An attempt was made to access a socket in a way forbidden by its access permissions.</code></p>
  
  <p>I'd appreciate it if anyone could work out a fix for that error or a better way to do a ping.</p>