dongmao7195 2017-08-13 20:13
浏览 20
已采纳

所有文件都应该有php扩展名或者php输出一个字符串吗? [关闭]

I'm wanting to show the server status of an ip on every page, but to check the status I need an php script. This script is what I found on the internet

<?php
    $server='google.be:80';
    $split=explode(':',$server);
    $ip=$split[0];
    $port=(empty($split[1]))?'80':$split[1];
    $server=$ip.':'.$port;
$fp = @fsockopen($ip, $port, $errno, $errstr, 1);
if($fp){
    echo $server.' is online';
    fclose($fp);
}
else{
    echo $server.' is offline';
}
?>

I want the echoes to be formatted like my css is formatted, so I could just replace the echoes with

?>
<p>Server is offline<p>
<?php

and

?>
<p>Server is online<p>
<?php

but then I would have to make every html file an php file, would you recommend that or is there a different way to handle this?

  • 写回答

3条回答 默认 最新

  • drxnfdx798517235 2017-08-13 20:26
    关注

    You could use jQuery AJAX to send the PHP data to your HTML page. You could json_encode the response and receive that data as a JSON object and get the data out of it.

    EDIT: In a production enviroment and for efficiency, it would be best if you convert the HTML files to PHP files, it will be worth the labour. However this little snippet below could be used for other functionality if modified or built upon so it's a learning experience for you to see basic jQuery AJAX calls.

    The following code is a working example of calling your PHP file and getting back the result. Seeing a basic example of using jQuery and AJAX will help you get a firm grounding of how to use it.

    check_server.php

    <?php
        $server='google.be:80';
        $split=explode(':',$server);
        $ip=$split[0];
        $port=(empty($split[1]))?'80':$split[1];
        $server=$ip.':'.$port;
    $fp = fsockopen($ip, $port, $errno, $errstr, 1);
    
       $result = new stdClass();
    if($fp){
        $result->result = 'success';
        fclose($fp);
    }
    else{
       $result->result = 'offline';
    }
    
    echo json_encode($result);
    ?>
    

    index.html

    <html>
        <head>
            <title>Website</title>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
    
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
            <script>
                $.ajax({
            url : "check_server.php",
            type : "POST",
            dataType: "json",
            success : function(results){
                          if (results.result === 'success')
                          {
                              $('#status').append('Server online.');
                          }
                          else
                          {
                             $('#status').append('Server offline.');
                          }
                      },
             error : function()
                  {
                      $('#status').append('An error has occurred.');
                  }
        });
                </script>
        </head>
        <body>
            <div id="status"></div>
        </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题