Home >Backend Development >PHP Tutorial >Detailed explanation of the case of PHP+Ajax detecting whether the network is normal
This article mainly introduces the relevant information of PHP Ajax to detect whether the network is normal. The example code is attached here. Friends in need can refer to it.
The specific implementation method is as follows:
html part code:
<!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 part code:
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; }
The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
jsDetectiontest网NetworkStatus Code Arrangement
##Linux How toCheckTestServerNetworkNetworkStatus
Detectiontest网网 Whether to specifically connect the function code_javascript skills
The above is the detailed content of Detailed explanation of the case of PHP+Ajax detecting whether the network is normal. For more information, please follow other related articles on the PHP Chinese website!