php实现的ping端口函数实例
程序员文章站
2022-10-04 19:17:01
本文实例讲述了php实现的ping端口函数。分享给大家供大家参考。
具体实现代码如下:
复制代码 代码如下:
<?php
/*
* @author xujiajay
* @date 2010-10-7
* @function 可以ping端口的php函数
*
*/
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 < 20;$i++)
{
if(stristr($array[$i],'server')){$k = $i; break;}
}
if(!stristr($array[$k],'server')) return 'unknown';
else return str_replace('server','服务器软件',$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 '<font color="#ff0000">'.getsoft($host,$port).'</font>';
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);
}
}
?>
<form method="post">
域名/ip:<input type="text" name="url" value="<?php echo $host;?>" size="50">
端口:<input type="text" name="duankou" value="<?php echo $port;?>" size="10">
<input type="submit" value="ping">
</form>
本文实例讲述了php实现的ping端口函数。分享给大家供大家参考。
具体实现代码如下:
复制代码 代码如下:
<?php
/*
* @author xujiajay
* @date 2010-10-7
* @function 可以ping端口的php函数
*
*/
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 < 20;$i++)
{
if(stristr($array[$i],'server')){$k = $i; break;}
}
if(!stristr($array[$k],'server')) return 'unknown';
else return str_replace('server','服务器软件',$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 '<font color="#ff0000">'.getsoft($host,$port).'</font>';
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);
}
}
?>
<form method="post">
域名/ip:<input type="text" name="url" value="<?php echo $host;?>" size="50">
端口:<input type="text" name="duankou" value="<?php echo $port;?>" size="10">
<input type="submit" value="ping">
</form>
希望本文所述对大家的php程序设计有所帮助。
上一篇: 老鼠不爱婚姻大事
下一篇: 人工智能拟人化 语言交流成核心