Example of a ping port function implemented by PHP, phpping Port instance
This article describes the ping port function implemented by PHP. Share to everyone for your reference.
The specific implementation code is as follows:
Copy CodeThe code is as follows:
<?php
/*
* @author Xujiajay
* @date 2010-10-7
* @function PHP functions that can ping ports
*
*/
Error_reporting (E_error);
Header ("content-type:text/html; Charset=utf-8 ");
Set_time_limit (120);
$host = isset ($_post[' url ')? Chop (Str_replace (' http://', ' ', $_post[' url ')), ' www.baidu.com ';
$port = isset ($_post[' Duankou ')? Chop ($_post[' Duankou '): ' 80 ';
$num = 10;
function Microtime_float ()
{
List ($usec, $sec) = Explode ("", Microtime ());
return (float) $usec + (float) $sec);
}
function Getsoft ($host, $port)
{
$fp = @fsockopen ($host, $port,& $errno,& $errstr, 3);
if (! $fp) return ' unknown ';
$get = "Get/http/1.1\r\nhost:". $host. " \r\nconnection:close\r\n\r\n ";
@fputs ($fp, $get);
$data = ";
while ($fp &&!feof ($fp))
$data. = Fread ($fp, 1024);
@fclose ($FP);
$array = explode ("\ n", $data);
$k = 2;
for ($i = 0; $i < $i + +)
{
if (Stristr ($array [$i], ' Server ') {$k = $i; break;}
}
if (!stristr ($array [$k], ' Server ')) return ' unknown ';
else return str_replace (' server ', ' Server Software ', $array [$k]);
}
function ping ($host, $port)
{
$time _start = Microtime_float ();
$ip = gethostbyname ($host);
$fp = @fsockopen ($host, $port,& $errno,& $errstr, 1);
if (! $fp) return ' Request timed out. ' \ r \ n ";
$get = "Get/http/1.1\r\nhost:". $host. " \r\nconnection:close\r\n\r\n ";
@fputs ($fp, $get);
@fclose ($FP);
$time _end = Microtime_float ();
$time = $time _end-$time _start;
$time = Ceil ($time * 1000);
Return ' Reply from ' $ip. ': Time= '. $time. ' MS ';
}
if (isset ($_post[' url ')) && isset ($_post[' Duankou '))
{
Echo '. Getsoft ($host, $port). ";
Echo ' Pinging '. $host. ' ['. gethostbyname ($host). '] With Port: '. $port. ' of data: '. ' \ r \ n ";
Ob_flush ();
Flush ();
for ($i = 0; $i < $num; $i + +)
{
Echo Ping ($host, $port);
Ob_flush ();
Flush ();
Sleep (1);
}
}
?>
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/910606.html www.bkjia.com true http://www.bkjia.com/PHPjc/910606.html techarticle PHP Implementation of the Ping port function instance, phpping Port instance This article describes the PHP implementation of the ping port function. Share to everyone for your reference. The specific implementation code is as follows: Copy code ...