How PHP Gets the ping time

Source: Internet
Author: User
This article mainly introduces the PHP to get ping time implementation of the relevant information, hope that through this article can help everyone, so that we can achieve such a function, the need for friends to refer to the next

PHP can use the EXEC function to execute shell commands to get ping time.

code example:


Echo ' Php_int_max: '. Php_int_max. "<br><br>";    $ip = ' 115.29.237.28 '; IP address if (path_separator== ': ')//linux{echo ' I am Linux '.     "<br><br>";  EXEC ("Ping-c 3-w 5 $ip", $info); Echo ' Linux info: '.  "<br>";     Print_r ($info);  $ping _time_line = end ($info); Echo ' <br><br>ping_time_line: '. $ping _time_line.     "<br><br>";  $ping _time = explode ("=", $ping _time_line) [1];  $ping _time_min = Explode ("/", $ping _time) [0]/1000.0;  $ping _time_avg = Explode ("/", $ping _time) [1]/1000.0;     $ping _time_max = Explode ("/", $ping _time) [2]/1000.0; Echo $ping _time_min. " " . $ping _time_avg. " " . $ping _time_max.   "<br><br>"; }else//windows{echo ' I am windows '.   "<br><br>";  EXEC ("Ping-c 3-w 5 $ip", $info); echo ' Windows info: '.  "<br>";     Print_r ($info);  $info _time = end ($info); Echo ' <br><br>info_time: '. $info _time. "<br><br>";}

Operation Result:


php_int_max:9223372036854775807

I am Linux

Linux Info:

Array ([0] = PING 115.29.237.28 (115.29.237.28) bytes of data. [1] = bytes from 115.29.237.28:icmp_seq=1 ttl=52 time=26.1 ms [2] = bytes from 115.29.237.28:icmp_seq=2 tt l=52 time=27.5 ms [3] = bytes from 115.29.237.28:icmp_seq=3 ttl=52 time=25.2 ms [4] = [5] =>-115.29.237.2 8 ping Statistics-[6] = 3 Packets transmitted, 3 received, 0% packet loss, time 2002ms [7] = RTT Min/avg/max/mde v = 25.280/26.339/27.590/0.970 ms)

Ping_time_line:rtt Min/avg/max/mdev = 25.280/26.339/27.590/0.970 ms

0.02528 0.026339 0.02759

Get ping time encapsulated into function


function Ping_time ($ip) {  $ping _cmd = "Ping-c 3-w 5". $ip;  EXEC ($ping _cmd, $info);  $ping _time_line = end ($info);     $ping _time = explode ("=", $ping _time_line) [1];  $ping _time_min = Explode ("/", $ping _time) [0]/1000.0;  $ping _time_avg = Explode ("/", $ping _time) [1]/1000.0;  $ping _time_max = Explode ("/", $ping _time) [2]/1000.0;     $result = Array ();  $result [' ping_min '] = $ping _time_min;  $result [' ping_avg '] = $ping _time_avg;  $result [' ping_max '] = $ping _time_max;     Print_r ($result);} Ping_time (' 115.29.237.28 ');

Operation Result:


Array ([ping_min] = 0.025497 [Ping_avg] = 0.025947 [Ping_max] = 0.026753)
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.