1

I want to do as the title states. To ping a users IP and return a result in ms, for instance:

Ping IP return 400ms.

I have no idea how to do this but I expect it would be relatively simple. I have access to the exec() function and the functions similar to it as I will be running this script on a virtual private server.

Thanks in advance.

h_12
  • 31
  • 1
  • 4

2 Answers2

1

try this

<?php

$out = array();
exec('ping -c 4 '.$_SERVER['REMOTE_ADDR'], $out);
print_r($out);

?>
Janis Lankovskis
  • 1,042
  • 9
  • 9
  • It seems to be taking a long time to load. – h_12 Sep 26 '11 at 13:42
  • Although I'd have wrapped this in a function call, it seems like this would work. You could get the wrong address if the user is browsing via a proxy, but that's a fairly simple addition if necessary - and I follow YAGNI. – Steve Hill Sep 26 '11 at 13:43
  • @h_12 - if you want this to be asynchronous, so as to not block page loading, then I suggest calling an Ajax script once the page has loaded; otherwise, you've always got the potential that it'll be slow. – Steve Hill Sep 26 '11 at 13:44
  • Yeah, it looks like it should work but at the moment the page doesn't want to load because I think the server is too busy pinging the IP and getting the output - is there some way to speed this process up or make it so that it only gives out one response rather than several? – h_12 Sep 26 '11 at 13:44
  • @h_12, see my second comment. You could also change the -c 4 to -c 1, but that would be less accurate. – Steve Hill Sep 26 '11 at 13:47
0

Try this:

<?php

$ip     = $_SERVER['SERVER_ADDR'];  // Get the IP address of the visitor
$result = system('ping -n 1 '.$ip, $retval); // the result contains the last line of the ping command.

if ($retval==0) echo "OK";
if ($retval==1) echo "NOT OK";

?>
devasia2112
  • 5,844
  • 6
  • 36
  • 56