I'm trying to ping the ip address to test the ip address whether it is available or not. But i having trouble on using the fsockopen function. I had do some research on using 'ping','exec' function but none of them seem really workable.
Here is my code to test the IP address:
<?php
$ipAddress = array("192.168.1.1","192.168.1.67");
$kiosk = array("Kuantan (35)","UTC Kuantan (36)");
$checkCount = 0;
foreach(array_combine($ipAddress,$kiosk) as $items => $kiosk){
$fp = @fSockOpen($items,80,$errno,$errstr,1);
if(is_resource($fp)){
if($fp) {
$status=0;
fclose($fp);
echo 'Success<br/>';
}
}
else{
echo 'Failed<br/>';
}
}
?>
Both the IP address i can ping from cmd prompt. But for 192.168.1.67 it lead me to 'Failed', only the 192.168.1.1. or 127.0.0.1 showing me 'Success'. Is there anything i do wrong?
答案 0 :(得分:0)
您的代码并没有真正ping通,只是尝试在端口80上打开TCP连接.ping命令使用ICMP数据包,因此192.168.1.67可能不接受端口80上的连接。
在与Catalyst聊天之后,这是一个示例代码,它只发送两个数据包,并等待最多2秒钟的响应。 $ retval在成功时为0,在丢包时为1,在任何其他错误时为2。这是在Linux上,windows命令参数有点不同,但你应该能够改变它。
<?
// unset variables first to avoid mixing the results from previous calls
$retval=-1;
$output=array();
exec("ping 127.0.0.1 -c2 -w2 2>&1",$output,$retval);
echo "Return code: ".$retval."<br>\n";
echo implode("<br>\n",$output);
?>