0

I wonder how to change the code so that the demand does not come from webhosting, but from client-browser and in this case show how many "ms" the corresponding client-browser has to a particular website/server

<?
function ping($host, $port, $timeout) { 
  $tB = microtime(true); 
  $fP = fSockOpen($host, $port, $errno, $errstr, $timeout); 
  if (!$fP) { return "Server is down"; } 
  $tA = microtime(true); 
  return round((($tA - $tB) * 1000), 0)." ms"; 
}

//Echoing it will display the ping if the host is up, if not it'll say "down".
echo ping("google.com", 80, 10); 
?>

1 Answers1

0

This is technically impossible. Especially, since you want to use PHP - you cannot make any PHP code execute on the client side.

The only way to measure latency on the client would be via JavaScript and you're very limited there due to the Same-Origin-Policy. You would need to have a latency measurement JavaScript on the server you want to measure a latency to (I do not speak of real "ping" here, because sending raw packets is also not possible - due to security reasons), as well as a WebSocket server on the very same host. Then you could establish a WebSocket connection through the JS that came from this server and evaluate the latency of establishing the WebSocket connection.

That's too far from a real ping or TCP handshake - your example isn't really ICMP either, so not a real ping as well. Plus, you cannot connect to any host of your choice from JavaScript, via WebSocket or AJAX request. So, what you want to achieve is impossible.

Damocles
  • 1,287
  • 1
  • 7
  • 9