Snippet. PHP. Checking If URL Exists

This is a simple function written in PHP to quickly check if an URL exists.

//======================== START OF FUNCTION ==========================//
// FUNCTION: url_exists                                                //
//=====================================================================//
function url_exists($url) {
    $headers = @get_headers($url);
    $response_code = substr($headers[0], 9, 3);
    if ($response_code == '200' or $response_code == '304') {
        return true;
    }
    return false;
}
//=====================================================================//
//  FUNCTION: url_exists                                              //
//========================= END OF FUNCTION ===========================//

Updated on: 23 Nov 2024