0

I am having issues getting my PHP script to display the response time to the IP of the visitor displaying in ms. I have already seen this: PHP - get server to ping a visitors IP and return the ping in ms

When I try to do some of the same code all mine outputs is

"8.8.8.8 is alive"

I'd actually like it to return the average round trip time or just the response time in milliseconds.

Here is my code that just outputs the above:

$pinginfo = array();
exec("/usr/sbin/ping -v -c 1 8.8.8.8", $pinginfo);
var_dump($pinginfo);
Community
  • 1
  • 1
devzspy
  • 29
  • 1
  • 3
  • Are you sure that's the right version of ping to use? Normal location for it is `/bin/ping`. – Marc B May 21 '12 at 05:24
  • I have tried /bin/ping didn't quite work. When I ran exec("which ping") didn't show it in /bin/ping – devzspy May 21 '12 at 06:21

1 Answers1

1

try saving your exec result to the $pinginfo

$pinginfo = exec("/bin/ping -v -c 1 8.8.8.8");
echo $pinginfo;

the way you are doing it now, you're only saving the integer return value. The exec function returns the last string from the executed stdout.

Developer
  • 2,021
  • 12
  • 11
  • Thank you for replying so quickly. I still receive the "8.8.8.8 is alive" message – devzspy May 21 '12 at 05:30
  • Which flavor of linux are you using?? – Developer May 21 '12 at 05:44
  • According to phpinfo() SunOS web01ppl01a 5.10 Generic_147441-11 i86pc – devzspy May 21 '12 at 06:19
  • It may be a problem with something else in your code. Can you post more of it? or maybe try making a smaller segment of code to test with and post that in entirety? – Developer May 21 '12 at 06:20
  • Here is a pastebin with my code: http://pastebin.com/iPGNM1KL - I wanted to post the info from the ping (round trip time) in the Google Maps annotation. – devzspy May 21 '12 at 06:33
  • When you run "/usr/sbin/ping -v -c 1 8.8.8.8" from the ssh of the machine, are you getting the results you're expecting? I think that the SunOS "ping" may be different than the command you're expecting. I don't have a SunOS machine to test this theory out on though. – Developer May 21 '12 at 06:37
  • I am not sure. The machine that the above code is being hosted on is my universities webspace they allow students to have. I have not tried to log in via ssh as a precautionary thing so I don't possibly break ToS. Thank you for your help though, it is much appreciated – devzspy May 21 '12 at 06:40