PHP判断服务器端口是否打开的代码_PHP教程

WBOY
發布: 2016-07-13 17:42:29
原創
800 人瀏覽過

记录一下,以后备用

<ol class="dp-xml">
<li class="alt"><span><span class="tag"></span><span class="tag-name">php</span><span> </span></span></li>
<li>
<span>$</span><span class="attribute">host</span><span> = </span><span class="attribute-value">www.google.com</span><span>; //要ping的地址,也可以是IP  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">port</span><span> = </span><span class="attribute-value">80</span><span>; //要ping的端口  </span>
</li>
<li>
<span>$</span><span class="attribute">num</span><span> = </span><span class="attribute-value">3</span><span>;  </span>
</li>
<li class="alt"><span>function microtime_float()  </span></li>
<li><span>{  </span></li>
<li class="alt"><span>list($usec, $sec) = explode(" ", microtime());  </span></li>
<li><span>return ((float)$usec + (float)$sec);  </span></li>
<li class="alt"><span>}  </span></li>
<li><span>function ping($host,$port)  </span></li>
<li class="alt"><span>{  </span></li>
<li>
<span>$</span><span class="attribute">time_start</span><span> = </span><span class="attribute-value">microtime_float</span><span>();  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">ip</span><span> = </span><span class="attribute-value">gethostbyname</span><span>($host);  </span>
</li>
<li>
<span>$</span><span class="attribute">fp</span><span> = @fsockopen($host,$port,&$errno,&$errstr,1);  </span>
</li>
<li class="alt"><span>if(!$fp) return replay time out!;  </span></li>
<li>
<span>$</span><span class="attribute">get</span><span> = </span><span class="attribute-value">"GET / HTTP/1.1
Host:"</span><span>.$host."
Connection: Close

";  </span>
</li>
<li class="alt"><span>@fputs($fp,$get);  </span></li>
<li><span>@fclose($fp);  </span></li>
<li class="alt">
<span>$</span><span class="attribute">time_end</span><span> = </span><span class="attribute-value">microtime_float</span><span>();  </span>
</li>
<li>
<span>$</span><span class="attribute">time</span><span> = $time_end - $time_start;  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">time</span><span> = </span><span class="attribute-value">ceil</span><span>($time * 1000);  </span>
</li>
<li>
<span>return Reply from .$ip.: </span><span class="attribute">time</span><span>=.$time.</span><span class="attribute-value">ms</span><span class="tag"><span class="tag-name">br</span><span> </span><span class="tag">/></span><span>;  </span></span>
</li>
<li class="alt"><span>}  </span></li>
<li>
<span>echo Pinging .$host. [.gethostbyname($host).] with Port:.$port. of data:</span><span class="tag"><span class="tag-name">br</span><span> </span><span class="tag">/></span><span class="tag"><span class="tag-name">br</span><span> </span><span class="tag">/></span><span>."
";  </span></span></span>
</li>
<li class="alt">
<span>for($</span><span class="attribute">i</span><span> = </span><span class="attribute-value">0</span><span>;$i </span><span class="tag"><span> $num;$i++)  </span></span>
</li>
<li><span>{  </span></li>
<li class="alt"><span>ping($host,$port);  </span></li>
<li><span>sleep(1);  </span></li>
<li class="alt"><span>ob_flush();  </span></li>
<li><span>flush();  </span></li>
<li class="alt"><span>}  </span></li>
<li>
<span class="tag">?></span><span> </span>
</li>
</ol>
登入後複製

本文摘自 http://www.cnblogs.com/luoine/archive/2010/12/01/1893156.html

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/486053.htmlTechArticle记录一下,以后备用 ? php $ host = www.google.com ;//要ping的地址,也可以是IP $ port = 80 ;//要ping的端口 $ num = 3 ; functionmicrotime_float() { list($usec,$s...
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板