IP and port test for ping in PHP

Source: Internet
Author: User

Because the friend said the port is not connected, deliberately tried the ping test with PHP, the result is quite satisfactory.

The code and the run diagram are as follows: (if there is a great God to improve the better)


$host = ' www.php100.com ';

$port = ' 80 ';

$num = 3; Ping times

Get time

function Mt_f () {

List ($usec, $sec) = Explode ("", Microtime ());

Return ((float) $usec + (float) $sec); Microseconds plus seconds


function Ping_f ($host, $port) {

$time _s = Mt_f ();

$ip = gethostbyname ($host);

$fp = @fsockopen ($host, $port);

if (! $fp)

Return to ' reply time out! ';

$get = "Get/http/1.1\r\nhost:" $host. " \r\nconnect: ". $port." Close\r\n ";

Fputs ($fp, $get);

Fclose ($FP);

$time _e = Mt_f ();

$time = $time _e-$time _s;

$time = Ceil ($time * 1000);

Return to ' reply from '. $ip. ': $port. ' time = '. $time. ' Ms<br/> ';


echo ' ping to '. $host. ' ['. gethostbyname ($host). '] With Port: '. $port. ' Data:<br/> ';

for ($i = 0; $i < $num; $i + +) {

Echo Ping_f ($host, $port);

1S per run intermediate interval

Sleep (1);

Refreshing output caching

Ob_flush ();

Flush ();



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.