Search code examples
phpcurlfile-exists

file_exists, @get_headers, cURL and detecting a favicon


After reading in various places, including http://php.net/manual/en/function.file-exists.php, I have the following working code which detects whether a given host eg. http://example.com has a favicon located at /favicon.ico

$file = 'http://www.easyjet.com/favicon.ico';
$file_headers = @get_headers($file);

if($file_headers[0] == 'HTTP/1.0 200 OK') {

    //place favicon as an image on the page
    echo "<img src ='" . $file . "'>";
    } else {
    //place default image
    echo  "<img src ='" . "globe.jpg" . "'>";

}

While I know there are better, more thorough ways to seek a favicon file on StackOverflow, I am more concerned about my use of @get_headers. Some of what I have read suggests cURL could be useful, or that I should set up a user agent.

Am I going to run into difficulty with this code in some cases that I have not foreseen?


Solution

  • Personaly I think that get_headers is the best way in this case. First of all, it performs HEAD request, which do less load to both your and remote host(as remote host only gives headers and your server doesn't need to fetch image itself).

    I don't really think this will cause any trouble and I haven't seen any issues caused by usage of that function.