具体实现方法如下:
html部分代码:
<!doctype html><html><head><title>php+ajax实时自动检测是否联网</title><meta http-equiv="content-type" content="text/html; charset=utf-8"><script type="text/javascript"><!--var xmlhttp;function createxmlhttprequest(){ if(window.activexobject){ xmlhttp = new activexobject("microsoft.xmlhttp"); } else if(window.xmlhttprequest){ xmlhttp = new xmlhttprequest(); }}function start(){ createxmlhttprequest(); var url="getnetlink"; xmlhttp.open("get",url,true); xmlhttp.onreadystatechange = callback; xmlhttp.send(null);}function callback(){ if(xmlhttp.readystate == 4){ if(xmlhttp.status == 200){ document.getelementbyid("shownetlink").innerhtml = xmlhttp.responsetext; settimeout("start()",8000); } }}// --></script></head><body onload="start();"><h1>php+ajax实时自动检测是否联网</h1><p>当前网络状态:<span id="shownetlink"></span></p></body></html>
php部分代码:
public function getnetlink(){ header("cache-control:no-cache,must-revalidate"); header("content-type:text/html;charset=utf-8"); $file=fopen("http://www.baidu.com/", "r"); if (!$file){ $shownetlink = "<font color=\"red\">网络连接失败</font>"; }else{ $shownetlink = "<font color=\"#06c\">网络连接正常</font>"; } echo $shownetlink; }
以上就是本文的全部内容,希望对大家的学习有所帮助。
相关推荐:
js检测网络状况代码整理
linux如何检测服务器网络状况
js检测网络是否具体连接功能的代码_javascript技巧
以上就是php+ajax 检测网络是否正常案例详解的详细内容。
