PHP - get server to ping a visitors IP and return the ping in ms

前端 未结 2 1020
说谎
说谎 2021-01-07 08: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 expe

相关标签:
2条回答
  • 2021-01-07 09:02

    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";
    
    ?>
    
    0 讨论(0)
  • 2021-01-07 09:07

    try this

    <?php
    
    $out = array();
    exec('ping -c 4 '.$_SERVER['REMOTE_ADDR'], $out);
    print_r($out);
    
    ?>
    
    0 讨论(0)
提交回复
热议问题