private function pingSocket() {
    // Create a package.
    $type = "\x08";
    $code = "\x00";
    $checksum = "\x00\x00";
    $identifier = "\x00\x00";
    $seq_number = "\x00\x00";
    $package = $type . $code . $checksum . $identifier . $seq_number . $this->data;

    // Calculate the checksum.
    $checksum = $this->calculateChecksum($package);

    // Finalize the package.
    $package = $type . $code . $checksum . $identifier . $seq_number . $this->data;

    // Create a socket, connect to server, then read socket and calculate.
    if ($socket = socket_create(AF_INET, SOCK_RAW, 1)) {
      socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array(
        'sec' => 10,
        'usec' => 0,
      ));
      // Prevent errors from being printed when host is unreachable.
      @socket_connect($socket, $this->host, null);
      $start = microtime(true);
      // Send the package.
      @socket_send($socket, $package, strlen($package), 0);
      if (socket_read($socket, 255) !== false) {
        $latency = microtime(true) - $start;
        $latency = round($latency * 1000);
      }
      else {
        $latency = false;
      }
    }
    else {
      $latency = false;
    }
    // Close the socket.
    socket_close($socket);
    return $latency;
  }
private function pingSocket() {
    // Create a package.
    $type = "\x08";
    $code = "\x00";
    $checksum = "\x00\x00";
    $identifier = "\x00\x00";
    $seq_number = "\x00\x00";
    $package = $type . $code . $checksum . $identifier . $seq_number . $this->data;

    // Calculate the checksum.
    $checksum = $this->calculateChecksum($package);

    // Finalize the package.
    $package = $type . $code . $checksum . $identifier . $seq_number . $this->data;

    // Create a socket, connect to server, then read socket and calculate.
    if ($socket = socket_create(AF_INET, SOCK_RAW, 1)) {
      socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array(
        'sec' => 10,
        'usec' => 0,
      ));
      // Prevent errors from being printed when host is unreachable.
      @socket_connect($socket, $this->host, null);
      $start = microtime(true);
      // Send the package.
      @socket_send($socket, $package, strlen($package), 0);
      if (socket_read($socket, 255) !== false) {
        $latency = microtime(true) - $start;
        $latency = round($latency * 1000);
      }
      else {
        $latency = false;
      }
    }
    else {
      $latency = false;
    }
    // Close the socket.
    socket_close($socket);
    return $latency;
  }
private function pingSocket ( ) { // Create a package. $ type = "\x08" ; $ code = "\x00" ; $ checksum = "\x00\x00" ; $ identifier = "\x00\x00" ; $ seq_number = "\x00\x00" ; $ package = $ type . $ code . $ checksum . $ identifier . $ seq_number . $ this -> data ; // Calculate the checksum. $ checksum = $ this -> calculateChecksum ( $ package ) ; // Finalize the package. $ package = $ type . $ code . $ checksum . $ identifier . $ seq_number . $ this -> data ; // Create a socket, connect to server, then read socket and calculate. if ( $ socket = socket_create ( AF_INET , SOCK_RAW , 1 ) ) { socket_set_option ( $ socket , SOL_SOCKET , SO_RCVTIMEO , array ( 'sec' => 10 , 'usec' => 0 , ) ) ; // Prevent errors from being printed when host is unreachable. @ socket_connect ( $ socket , $ this -> host , null ) ; $ start = microtime ( true ) ; // Send the package. @ socket_send ( $ socket , $ package , strlen ( $ package ) , 0 ) ; if ( socket_read ( $ socket , 255 ) !== false ) { $ latency = microtime ( true ) - $ start ; $ latency = round ( $ latency * 1000 ) ; } else { $ latency = false ; } } else { $ latency = false ; } // Close the socket. socket_close ( $ socket ) ; return $ latency ; }
The socket method uses raw network packet data to try sending an ICMP ping
packet to a server, then measures the response time. Using this method
requires the script to be run with root privileges, though, so this method
only works reliably on Windows systems and on Linux servers where the
script is not being run as a web user.

@return int
Latency, in ms.
The socket method uses raw network packet data to try sending an ICMP ping packet to a server then measures the response time . Using this method requires the script to be run with root privileges though so this method only works reliably on Windows systems and on Linux servers where the script is not being run as a web user .