Home >Backend Development >PHP Tutorial >PHP Ajax real-time method to automatically detect whether the Internet is connected, ajax real-time_PHP tutorial

PHP Ajax real-time method to automatically detect whether the Internet is connected, ajax real-time_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:48:05764browse

How to automatically detect whether PHP Ajax is connected to the Internet in real time, ajax real-time

The example in this article describes the method of PHP Ajax to automatically detect whether it is connected to the Internet in real time. Share it with everyone for your reference. The specific implementation method is as follows:

html part of the code:

<!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>

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; 
}

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1024912.htmlTechArticleHow PHP Ajax automatically detects whether it is connected to the Internet in real time, ajax real-time This article describes how PHP Ajax automatically detects whether it is connected to the Internet in real time method. Share it with everyone for your reference. Specific implementation method...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn