Jump to content

determining if any url has a favicon (favicon.ico)


dsaba

Recommended Posts

Hi I'm trying to construct an algorithm that will be able to find the favicon for any url IF it has one. Usually favicons are on the root of the base page your are viewing and titled 'favicon.ico'

 

ie:

http://us2.php.net/str_replace  will have a favicon at: http://us2.php.net/favicon.ico

 

Not all webpages do this, and some simply state the location (local or remote) of their favicon address in the  html meta tags. ie: <meta shortcut icon="http://url/favicon.ico">

 

I have a problem correctly identifying the favicon as an image itself, through php functions like getimagesize()

and file_exists()

 

This favicon will identify as an image with getimagesize() 'http://us2.php.net/favicon.ico'

but 'http://imar.spaanjaars.com/favicon.ico' will not.

 

What about file_exists().. well 'http://imar.spaanjaars.com/favicon.ico' will return FALSE with that function as well.. even though it DOES exist.

 

So I was thinking why not read the headers of the favicon file itself to determine if it is favicon file:

This website: http://filext.com/file-extension/ICO  says you can identify it by the hex headers:

Hex: 00 00 01 00

 

BUT, this is not true for all favicon icons in use as you can see in php's favicon icon's headers:

42 4d 36 03 00

 

 

Anyone have any ideas?

This is all I got to really work so far, but it is too intense because it will request the contents of the image through file_get_contents()...

So what I'm really asking is: Is there a less resource intensive way to determine if a remote file is NOT an html file or contains html tags?

 

<?php
function findLocalFavicon($url, $icoStr = 'favicon.ico') {
$url = str_replace('http://', '', $url);
$found = true;
while ($found !== false) { //while not false
	$url = dirname($url);
	if ($url == '.') {
		break;
	}
	$favicon = 'http://'.$url.'/'.$icoStr;
	$found = strpos(@file_get_contents($favicon), 'html');
}

if ($found === FALSE) {
	return $favicon;
} else {
	return false;
}
}

?>

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.