Heim > Backend-Entwicklung > PHP-Tutorial > PHP+Ajax 检测网络是否正常实例详解

PHP+Ajax 检测网络是否正常实例详解

PHPz
Freigeben: 2018-09-30 17:51:24
nach vorne
1036 Leute haben es durchsucht

这篇文章主要介绍了PHP+Ajax 检测网络是否正常实例详解的相关资料,这里附有实例代码,需要的朋友可以参考下

本文实例讲述了PHP+Ajax实时自动检测是否联网的方法。分享给大家供大家参考。具体实现方法如下:

html部分代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<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>
Nach dem Login kopieren

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; 
}
Nach dem Login kopieren

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

更多相关课程请访问 php编程从入门到精通全套视频教程

 

Verwandte Etiketten:
Quelle:jb51.net
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage