干货分享,灵感创作,不定期更新,敬请期待。

php函数模拟ping

php xhq 2044℃ 0评论

php模拟ping,实际上是端口扫描
<!DOCTYPE html>
<html>
<head>
<title>time of port</title>
</head>
<body>
<div id=”main”>
<form action=”” method=”get”>
<input type=”text” id=”input” name=”HOST” value=”” placeholder=”www.baidu.com”>
<input type=”text” name=”PORT” placeholder=”default is 80″>
<input type=”text” name=”NUM” placeholder=”default is 4″>
<input type=”submit”>
</form>
</div>
<?php

// $host = ‘www.php100.com’;

if(isset($_GET[‘HOST’])){
$host = $_GET[‘HOST’];
if(!$_GET[‘PORT’]==””){
$port = $_GET[‘PORT’];
}else{
$port = ’80’;//端口默认值
}

if(!($_GET[‘NUM’]==””)){
$num = $_GET[‘NUM’];
}else{
$num = 4; //Ping次数
}
//获取时间

function mt_f (){

list($usec,$sec) = explode(” “,microtime());

return ((float)$usec + (float)$sec); //微秒加秒

}

function ping_f($host,$port){

$time_s = mt_f();

$ip = gethostbyname($host);

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

if(!$fp){

echo ‘<span style=”color:red;”>reply time out!</span><br>’;
return;
}

$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 ‘reply from ‘.$ip.’:’.$port.’ time = ‘.$time.’ms<br />’;

}

echo ‘scan for ‘.$host.’ [‘.gethostbyname($host).’] with port:’.$port.’ of data:<br />’;

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

echo ping_f($host,$port);

//每次运行中间间隔1S

sleep(1);

//刷新输出缓存

ob_flush();

flush();

}
sleep(1);
echo ‘finish!’;
}
?>

<script type=”text/javascript”>
window.onload=function() {
document.getElementById(“input”).focus();
}
</script>
</body>
</html>

转载请注明:爱游思 » php函数模拟ping

喜欢 (0)

您必须 登录 才能发表评论!