Check if favicon.ico exists on a remote server

Discussion in 'PHP' started by itssangy, Apr 28, 2007.

  1. #1
    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
     
    itssangy, Apr 28, 2007 IP
  2. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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
     
    bobby9101, Apr 28, 2007 IP
  3. itssangy

    itssangy Peon

    Messages:
    135
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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 :)
     
    itssangy, Apr 28, 2007 IP
  4. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #4
    then do a search in the head for <link... and another for a favicon.ico file in the root and compare them
     
    bobby9101, Apr 28, 2007 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    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:
     
    nico_swd, Apr 28, 2007 IP
  6. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #6
    unfortunately most hosts don't have allow_url_fopen enabled :(
     
    bobby9101, Apr 28, 2007 IP
  7. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #7
    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...
     
    nico_swd, Apr 28, 2007 IP
  8. itssangy

    itssangy Peon

    Messages:
    135
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #8
    thanks everyone + nico for the help :)
     
    itssangy, Apr 28, 2007 IP