(This is intended for PHP 5)
<?php
/**
* Check if an external URL is valid
*
* @param string $path
* @return boolean
*/
function check_path($path){
$headers = @get_headers($path);
if (preg_match("/200/", $headers[0])){
return true;
}
return false;
}
?>
In a nutshell it tries to fetch the HTTP headers for the given path/URL from the webserver hosting it, and if the response code is "200" (also known as "OK") - which indicate that the request has been received, understood, accepted and processed successfully. (The @ in front of the function call is PHP's error control operator - which tells it to ignore any errors/warnings it might generate.)
The same method can also be used to check content size (in bytes):
<?php $headers = @get_headers($path, 1); return $headers['Content-Length']; ?>
The second parameter to get_headers() simply tells it to parse the result and set the array key's to the corresponding header field names instead of numerical keys - which is a more reliable way of doing it. Note that even with this, the HTTP response code will always be key 0 in the array - for the sake of consistency. Thus we do not need to concern ourselves with the keys in the initial function described above.












