问题
I want the users of my website to check if any other website (http and/or https) is up. There are sites out there that use google analytics for that (if I understood it right). But I don't understand how they do it.
Question 1) How do I use google-analytics on my website to check if some other site is up?
Question 2) How do I do it by myself? Using php or javascript? I wonder if google-analytics might be more reliable in terms if they use multiple server locations to check whether the site is online compared to a single location that I would use with my own code.
回答1:
You can use server side Curl and monitor http response header, site timeouts.
回答2:
One can try to connect directly to the http(s) port of the server.
$canConnect = FALSE;
$host = 'www.example.com';
$service_port = 80; // http, for https use 443;
$address = gethostbyname ($host);
$socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket !== FALSE) {
$result = socket_connect ($socket, $address, $service_port);
if ($result) {
$canConnect = TRUE;
}
socket_close($socket);
}
回答3:
You could ping the servers and monitor the responses. This link shows you the implementation in PHP: http://www.darian-brown.com/php-ping-script-to-check-remote-server-or-website/
来源:https://stackoverflow.com/questions/11482513/how-to-check-from-php-html-if-website-is-up