2012-01-12 68 views
1

我还是比较新的PHP,你可以评论下面的代码是否有好处来判断一个网站是否正常运行,以及它是否可能不适合原因和更好的选择?php get_headers一个很好的方法来判断一个网站是否启动?

在此先感谢。

$siteHeader = @get_headers($url , 1); 
if ($siteHeader > 1) { 
    $siteUp = true; 
} else { 
    $siteUp = false; 
} 
+0

get_headers不一致:http://stackoverflow.com/questions/12781795/get-headers-inconsistency – Baba 2012-10-08 15:38:10

回答

1

更新:我越觉得它的以下的罚款看起来。我已经扩展了我的初步答案,回想起来这很简单。

基本的用法很好,但是您可能还想检查HTTP响应代码,而不是仅仅检查是否得到响应。现在的代码的方式,它只是告诉你,有人在另一边倾听,这是大多数人认为“该网站是”的很长一段路。

下面介绍如何轻松地隔离的HTTP响应代码(或获得false如果请求失败):

$headers = get_headers('http://www.google.com'); 
$code = $headers ? intval(end(explode(' ', $headers[0], 2))) : false; 

除此之外,这里还有重定向的事情:如果你看到一个重定向你会怎么做?您查询的服务器可能正常,但您重定向到的服务器可能已关闭。如果有人在浏览器中键入URL,他们将被重定向并最终超时,而单步测试会说一切正常。如果有重定向循环呢?浏览器会检测到这个并最终超时,但是你需要编写相当多的代码才能做到这一点。

因此,最终cURL确实看起来像唯一的保证解决方案,因为它透明地完成了这一切。

+0

get_headers不一致:HTTP: //stackoverflow.com/questions/12781795/get-headers-inconsistency ..它可能不是最好的选择..我现在在curl上运行类似的测试 – Baba 2012-10-08 15:39:31

1

根据你的情况,但特别是如果你的网址是由用户提交的,我会去做类似的事情。

//returns true, if domain is availible, false if not 
function isDomainAvailible($domain) 
{ 
    //check if URL is valid 
    if(!filter_var($domain, FILTER_VALIDATE_URL)){ 
      return false; 
    } 

    $agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"; 
    $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_VERBOSE, FALSE); 
    curl_setopt ($ch, CURLOPT_TIMEOUT, 10); 
    curl_setopt ($ch,CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt ($ch,CURLOPT_SSLVERSION, 3); 
    curl_setopt ($ch,CURLOPT_SSL_VERIFYHOST, FALSE); 
    $page=curl_exec($ch); 
    //echo curl_error($ch); 
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
    curl_close($ch); 
    if ($httpcode >= 200 && $httpcode < 300) 
    return true; 
    else 
    return false; 
} 

主要是因为如果您不发送useragent,某些服务器不会响应。

+0

“主要是因为一些服务器不会响应,如果你不发送用户代理。” - 这是真的吗?即使是这样,卷曲也许是矫枉过正的。我会用PHP套接字编写一个解决方案,但我想这已经足够了。 – 2012-01-12 22:26:10

+0

@JohnChadwick是的。作为一项安全措施:) – 2012-01-12 22:27:32

2

我使用卷曲,但是那只是我:

function check($url,$ignore = ''){ 
$agent = "Mozilla/4.0 (B*U*S)"; 
$ch = curl_init(); 
curl_setopt($ch,CURLOPT_URL,$url); 
curl_setopt($ch,CURLOPT_USERAGENT,$agent); 

curl_setopt($ch,CURLOPT_VERBOSE,false); 
curl_setopt($ch,CURLOPT_TIMEOUT,45); 
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,45); 

curl_setopt($ch,CURLOPT_HEADER,true); 
curl_setopt($ch,CURLOPT_NOBODY,true); 

curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); 
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true); 
curl_setopt($ch,CURLOPT_MAXREDIRS,5); //follow up to 10 redirections - avoids loops 


curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0); //fix for certificate issue 
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0); //fix for certificate issue 


$page = curl_exec($ch); 
$err = curl_error($ch); 
$httpcode = curl_getinfo($ch,CURLINFO_HTTP_CODE); 
curl_close($ch); 

$codes = array(
    0=>'Domain Not Found',100=>'Continue',101=>'Switching Protocols',200=>'OK',201=>'Created',202=>'Accepted',203=>'Non-Authoritative Information',204=>'No Content',205=>'Reset Content',206=>'Partial Content',300=>'Multiple Choices',301=>'Moved Permanently',302=>'Found',303=>'See Other',304=>'Not Modified',305=>'Use Proxy',307=>'Temporary Redirect',400=>'Bad Request',401=>'Unauthorized',402=>'Payment Required',403=>'Forbidden',404=>'Not Found',405=>'Method Not Allowed',406=>'Not Acceptable',407=>'Proxy Authentication Required',408=>'Request Timeout',409=>'Conflict',410=>'Gone',411=>'Length Required',412=>'Precondition Failed',413=>'Request Entity Too Large',414=>'Request-URI Too Long',415=>'Unsupported Media Type',416=>'Requested Range Not Satisfiable',417=>'Expectation Failed',500=>'Internal Server Error',501=>'Not Implemented',502=>'Bad Gateway',503=>'Service Unavailable',504=>'Gateway Timeout',505=>'HTTP Version Not Supported'); 

$httpcode_out = 'http: '.$httpcode.' ('.$codes[$httpcode].')'; 
$err = 'curl error: '.$err; 

$out = array(
    $url,$httpcode_out,$err); 

if($httpcode>=200&&$httpcode<307){//good 
    return array('Work',$out); 
}else{//BAD 
    return array('Fail',$out); 
} 

}

相关问题