Home > Backend Development > PHP Tutorial > PHP connects to Baidu effect through Ajax call to realize the function of detecting whether the website is connected to the Internet

PHP connects to Baidu effect through Ajax call to realize the function of detecting whether the website is connected to the Internet

墨辰丷
Release: 2023-03-31 07:48:01
Original
1695 people have browsed it

This article mainly introduces the function of PHP to connect to Baidu through Ajax call to detect whether the website is connected to the Internet. Interested friends can refer to it. I hope it will be helpful to everyone.

The example in this article describes the method of PHP Ajax automatically detecting whether it is connected to the Internet in real time. The specific implementation method is as follows:

Part of the html 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>
Copy after login

Part of the php 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; 
}
Copy after login

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

php definition and implementation of event mechanism

php implements skin change based on cookies Method

Three common uses of php to simulate post requests

The above is the detailed content of PHP connects to Baidu effect through Ajax call to realize the function of detecting whether the website is connected to the Internet. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template