简体   繁体   中英

Server1 display when Server2 is offline

I'm just looking for the best way to approach this issue.

Issue: Server1 displays when Server2 is offline

What I've done, but I'm not happy with the result below.

PHP:

function ping ($host, $timeout = 1) {
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$host);
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,1);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    $result = curl_exec($ch);
    curl_close($ch);

    return $result;
}


$ping = ping('server2.domain.com');
if($ping==false){
  $warningText = "Server2 is not available.";
  echo "<li class='navbar-text hidden-xs'><span style='color:#d9534f;' id='warning' class='glyphicon glyphicon-warning-sign' data-toggle='tooltip' title='" . $warningText . "' data-placement='bottom'></span></li>";
}

The result is the website hangs for quite a bit while processing this ping request if the server doesn't respond at all (because it's offline).

Should I load the page and do an ajax onload, or is there another method that would work better for this?

I own both servers, they're external to eachother in a sub domain (domain.com checking server2.domain.com).

You could either do an AJAX request to a php script to check that or make a cronjob to check the server all 10mins (for example) (you can't directly check with a cronjob that, you have to do that with a (php) script) and save the result in a file. And in your script you're just loading the file and read what's written in there and then show either the server is online or the server isn't.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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