PHP - get server to ping a visitors IP and return

2019-02-15 23:24发布

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.

标签: php ip ping
2条回答
手持菜刀,她持情操
2楼-- · 2019-02-16 00:03

try this

<?php

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

?>
查看更多
叛逆
3楼-- · 2019-02-16 00:30

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";

?>
查看更多
登录 后发表回答