简体   繁体   English

单独加载页面的一部分吗?

[英]Load a section of page separately from the rest?

I want to check the status of a couple of servers and display on a page if they're up or down. 我想检查几个服务器的状态,如果它们处于启动或关闭状态,则显示在页面上。 Currently, I'm doing this with PHP's fsockopen() function. 目前,我正在使用PHP的fsockopen()函数来执行此操作。 As part of this function, you determine a timeout time. 作为此功能的一部分,您可以确定超时时间。 This means that if the servers are down, it'll block for say 2 seconds before moving on. 这意味着,如果服务器宕机,它将阻塞2秒钟,然后再继续运行。 This causes my webpage to wait till the fsockopen() s are done before it displays the page. 这使我的网页在显示页面之前一直等到fsockopen()完成。

Is there a way to get these fsockopen() functions to run separately from the rendering of the page, and have their results included in after they're determined? 有没有办法让这些fsockopen()函数与页面的呈现分开运行,并在确定结果后将其结果包括在内? It doesn't have to be PHP, I guess. 我猜不一定是PHP。

Split this into two sub-problems: 将此分为两个子问题:

1) Create a cron job (or otherwise schedule) a script that will check the server status and save that result somewhere (db, cache, file, post-it, etc). 1)创建一个cron作业(或以其他方式安排)一个脚本,该脚本将检查服务器状态并将结果保存在某处(数据库,缓存,文件,便利贴等)。 Run it as frequently as you like. 尽可能频繁地运行它。

2) Query that result either while rendering the page or via an AJAX call. 2)在呈现页面时或通过AJAX调用查询结果。

Use AJAX. 使用AJAX。 I'd recommend using jQuery's $.ajax function ( http://api.jquery.com/jQuery.ajax/ ) to call a PHP script which will query a server. 我建议使用jQuery的$ .ajax函数( http://api.jquery.com/jQuery.ajax/ )调用将查询服务器的PHP脚本。 You will provide a success function to the ajax call which will be executed when the ajax call has finished. 您将为ajax调用提供成功函数,该函数将在ajax调用完成后执行。

Create a php page that accepts a variable to know which server to check 创建一个php页面,该页面接受一个变量来知道要检查哪个服务器

<?php
$server = isset($_GET['server']) ? $_GET['server'] : '';
$upordown = 'invalid server'; //default incase wrong variable is set

switch ($server) {
  case 'server1':
    //check server 1; EG: $upordown = checkserver('http://www.example.com');
    break;
  case 'server2':
    //check server 2;
    break;
  case 'server3':
    //check server 3;
    break;
}

echo $upordown
?>

and then on your show page using jquery: 然后在显示页面上使用jquery:

<html>
<head>
  <script src='jquery.js'></script>
  <script>
  $(function(){
    $('.serverstatus').each(function() {
      var id = $(this).data('serverid');
      $(this).load('/servercheck.php?server='+id);
    });
  });
  </script>
</head>
<body>
  <div class='serverstatus' data-serverid='server1'>loading</div>
  <div class='serverstatus' data-serverid='server2'>loading</div>
  <div class='serverstatus' data-serverid='server3'>loading</div>
</body>
</html>

Now you could add some extra safety like caching and random servernames instead of 1,2,3 现在,您可以添加一些额外的安全性,例如缓存和随机服务器名,而不是1,2,3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM