用PHP监控网站在线状态

<?php
/**
*
* @param string $url
* @return boolean
*/
function SiteMonitorBot($url) {

$agent = ‘Mozilla/5.0 (compatible; SiteMonitorBot/1.0; +http://www.xiaokyun.com)’;

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

$content = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);

//echo $http_code.'<br>’;

if (($http_code >= 200 && $http_code <= 300) // (!empty($content) &&
|| $http_code == 301 || $http_code == 302 || $http_code == 307) { //允许跳转
return true;
} else {
return false;
}
}

ignore_user_abort(true);
set_time_limit(0);
while (true) {
SiteMonitorBot(‘http://www.xiaokyun.com’);
sleep(900); //每隔15分钟自动运行
}

?>

Published by

@XiaoKyun

双鱼男,过分热心的好人。