background image

PHP+Ajax 实时自动检测是否联网的实现代码

分享一例

php 与 ajax 结合实现的自动检测是否联网的代码,使用 fopen 方法进行相关的判

断,不错的小例子,有需要的朋友参考下吧。

1,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 实时自动检测是否联网-www.jbxue.com</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>