簡單解釋一下上面的代碼。get_headers的作用就是訪問一個遠程地址,把服務器發送的HTTP頭以數組形式返回。而$header[0]則是服務器返回的狀態碼(如果不出意外的話狀態碼應該都是第一個返回的)。
要確定一個文件在遠端服務器上存在,只需要確定訪問這個文件返回的狀態碼是”HTTP/1.1 200 OK”就行了(當然你也可以判斷如果狀態碼不是”HTTP/1.1 404 Not Found”的話則文件存在,不過總感覺不保險,畢竟還有其他的諸如301,400這類的狀態碼)。
獲取三位HTTP響應碼的例子:
<?php function get_http_response_code($theURL) { $headers = get_headers($theURL); return substr($headers[0], 9, 3);
} ?>
排除重定向的例子:
<?php/*** 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 optionsif (isset($opts)) {$opts = array('http' =>array('max_redirects'=>20,'ignore_errors'=>0));stream_context_get_default($opts);}//returnreturn $headers;
}?>
本文固定鏈接: http://www.xssxss.com/fuck/323.xss | Shine的圣天堂-〃敏〃