Hi, I was wondering if there's a quick way to check to see if the file favicon.ico exists on a remote server. I was able to check by using cURL to download the header and look for the content type "image/x-icon", but some sites don't show that, such as www.digitalpoint.com/favicon.ico (which prompts you to download the file instead). Any help would be appreciated. Thank you
I would use file_get_contents and search through the file for "shortcut icon" or something like that, then grab that url and use file_exists to see if it exists
some sites have the shortcut icon in the <link rel tag, but other sites don't. the favicon.ico is in the root folder. so it's not efficient to do it that way. Thanks for the suggestion anyhow
then do a search in the head for <link... and another for a favicon.ico file in the root and compare them
Randomly assuming you have PHP 5 and allow_url_fopen enabled in php.ini function favicon_exists($url) { @extract(@parse_url($url)); if ($headers = @get_headers($scheme . '://' . $host . (substr(@$path, -1) == '/' ? $path : '/') . 'favicon.ico') AND preg_match('/^HTTP\/1\.\d\s+200/', trim($headers[0]))) { return true; } return ($content = @file_get_contents($url) AND preg_match('/<link[^>]+rel\s*=\s*["\']?\s*shortcut\s+icon\s*["\']?/i', $content)); } PHP:
All hosts I've been on so far had it enabled. However, here a version that tries with cURL if allow_url_fopen is disabled. function favicon_exists($url) { @extract(@parse_url($url)); $faviconurl = $scheme .'://' . $host . (substr(@$path, -1) == '/' ? $path : '/') . 'favicon.ico'; if (ini_get('allow_url_fopen') AND function_exists('get_headers')) { $headers = @get_headers($faviconurl); } else if (function_exists('curl_init') AND $ch = @curl_init($faviconurl)) { curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_NOBODY, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $headers = explode("\n", curl_exec($ch)); curl_close($ch); } if (isset($headers[0]) AND preg_match('/^HTTP\/1\.\d\s+200/', trim($headers[0]))) { return true; } $pattern = '/<link[^>]+rel\s*=\s*["\']?\s*shortcut\s+icon\s*["\']?/i'; if (ini_get('allow_url_fopen')) { return ($content = @file_get_contents($url) AND preg_match($pattern, $content)); } else if (function_exists('curl_init') AND $ch = @curl_init($url)) { curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); return preg_match($pattern, curl_exec($ch)); } return false; } PHP: Not sure if get_headers() requires allow_url_fopen though...