簡單解釋一下上面的代碼。get_headers的作用就是訪問一個遠程地址,把伺服器發送的HTTP頭以數組形式返回。而$header[0]則是伺服器返回的狀態代碼(如果不出意外的話狀態代碼應該都是第一個返回的)。
要確定一個檔案在遠端伺服器上存在,只需要確定訪問這個檔案返回的狀態代碼是”HTTP/1.1 200 OK”就行了(當然你也可以判斷如果狀態代碼不是”HTTP/1.1 404 Not Found”的話則檔案存在,不過總感覺不保險,畢竟還有其他的諸如301,400這類的狀態代碼)。
擷取三位HTTP響應碼的例子:
function get_http_response_code($theURL) {
$headers = get_headers($theURL);
return substr($headers[0], 9, 3);
}
?>
排除重新導向的例子:
/**
* Fetches all the real headers sent by the server in response to a HTTP request without redirects
* 擷取不包含重新導向的前序
*/
function get_real_headers($url,$format=0,$follow_redirect=0) {
if (!$follow_redirect) {
//set new default options
$opts = array('http' =>
array('max_redirects'=>1,'ignore_errors'=>1)
);
stream_context_get_default($opts);
}
//get headers
$headers=get_headers($url,$format);
//restore default options
if (isset($opts)) {
$opts = array('http' =>
array('max_redirects'=>20,'ignore_errors'=>0)
);
stream_context_get_default($opts);
}
//return
return $headers;
}
?>
本文固定連結: http://www.xssxss.com/fuck/323.xss | Shine的聖天堂-〃敏〃
http://www.bkjia.com/PHPjc/478544.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/478544.htmlTechArticle簡單解釋一下上面的代碼。get_headers的作用就是訪問一個遠程地址,把伺服器發送的HTTP頭以數組形式返回。而$header[0]則是伺服器返回的狀態...