PHP:端口 ping 上的“离线”占位符 - 例如:加载页面,回显“未知”,查询端口,将第一个回显替换为“在线”或“离


PHP: "Offline" placeholder on port ping - Ex: Load page, echo "unknown", query port, replace first echo with "online" or "offline"?

这是我目前拥有的(不包括CSS,它都是在服务器端设置的):

<?php 
    $getter = $_GET['site'];
    ##EXAMPLE VARIABLE:
    if ($getter == 'URLinput')
   { $site = test_port('URLinput.com',80,4); }
    function test_port($host,$port=80,$timeout=1)
    {
            $fsock = fsockopen($host, $port, $errno, $errstr, $timeout);
            if ( ! $fsock )
            {
                    return FALSE;
            }
            else
            {
                    return TRUE;
            }
    }
    ?>
    ##HEADER STUFF
    <?php
    if ($site == 1)
        { $status = $online;
        } else if ($site == 0) {
        $status = $offline;
        } else {
        $status = $unknown;
    }
    echo $status;
    ?>

这非常适合显示超时为 4 的联机或脱机。我想做的是在等待服务器返回"端口联机"或"端口脱机"时显示"未知"。

这方面的一个例子:http://status.spout.org/

您可以看到(刷新时)在右侧,如果服务器正在等待响应,它将显示未知。

如何让它加载页面、回显$unknown、运行函数、删除第一个回显和回显$status?

我是PHP的中级初学者,所以请务必彻底简要介绍;D谢谢!-大卫

以下是使用

curl 和 jQuery 的方法,使用 jQuery,您可以在检查结果后轮询服务器以获取结果,结果保存在会话中,以免重复该过程。希望它有帮助:

<?php 
/*Start a session to hold the results this will stop
  multiple online checks from the same user.
 */
session_start();
//The urls you want to check
$urls = array('example'=>'http://example.com',
              'stackoverflow'=>'http://stackoverflow.com',
              'test'=>'http://test.com',
              'google'=>'http://google.com');
//Is it an ajax request from jQuery
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && 
   strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest'){
    //Set the return content type
    header('Content-Type: application/json');
    //Cache the online checks in user session and for 1 hour on filesystem
    if(!isset($_SESSION['status'])){
        if(file_exists('cache.txt') && (time() - 3600 < filemtime('cache.txt'))){
            $_SESSION['status'] = json_decode(file_get_contents('cache.txt'),true);
        }else{
            $_SESSION['status'] = array();
            //Loop through the $urls array and ping the site using the curl function below
            foreach($urls as $key=>$url){
                $_SESSION['status'][$key]=get_site($url);
            }
            //Update cache
            file_put_contents('cache.txt',json_encode($_SESSION['status']));
        }
    }
    //Echo the json string back to jQuery
    echo json_encode($_SESSION['status']);
    die;
}
//The curl function to check if the site is online
function get_site($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_TIMEOUT,2);
    curl_exec($ch);
    if(curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200){
        return 'Online';
    }else{
        return 'Offline';
    }
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Online Check</title>
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script>
function pollresults(){
    setTimeout(function(){
        $.ajax({ url: "./test_server.online.php", cache: false,
        success: function(data){
            <?php
            //Loop through the $urls array and build a simple replaceWith
            foreach($urls as $key=>$value){
                echo '$("#'.$key.'").replaceWith("<p id='"'.$key.''">"+data.'.$key.'+"</p>");'.PHP_EOL;
            }
            ?>
            pollresults();
        }, dataType: "json"});
    }, 1000);
}
$(document).ready(function(){
    pollresults();
});
</script>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" width="50%">
  <tr>
    <td width="50%">Site:</td>
    <td width="50%">Status:</td>
  </tr>
<?php foreach($urls as $key=>$value):?>
<tr>
    <td width="50%"><a href="<?php echo $value;?>"><?php echo $value;?></a></td>
    <td width="50%"><p id="<?php echo $key;?>">Checking</p></td>
</tr>
<?php endforeach;?>
</table>
</body>
</html>

只需将脚本复制并粘贴到名为 test_server.online 的文件中.php然后进行测试即可。