Forum Moderators: coopster

Message Too Old, No Replies

check if external file exists for use with favicons

         

formasfunction

7:43 pm on Oct 1, 2007 (gmt 0)

10+ Year Member



I'm trying to add favicons for external links on my site and I'd like to serve up a default image of my own if one doesn't exist. I've seen a javascript solution out there but I'd rather work this out in php. Here's what I have that's working:


$urlbits = parse_url($array['url']);
$favicon = "http://". $urlbits['host'] ."/favicon.ico";
if (!@fclose(@fopen($favicon, "r"))) {
$favicon = "/dev/images/favicondefault.gif";
}

The problem is that the script is incredibly slow as it waits for every single server in the list (typically 15 or so) to respond. If I just go ahead and print the page then the available favicons display as they load but then I can't substitute in a default favicon if one doesn't exist.

So, my question is, is there a better script for what I'm doing or a way to adjust the timeout for my current script so that a slow external server doesn't cause it to hang?

PHP_Chimp

7:50 pm on Oct 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I dont have an answer for your timings; however what about storing the favicons so you dont have to search for them each time?
Then if you have a copy serve there favicon if you dont have a copy then serve your default one.

formasfunction

8:38 pm on Oct 1, 2007 (gmt 0)

10+ Year Member



That's probably a better idea. I'd love to see if anyone else has an answer to my original question but I'll probably go ahead and just store them for reliability.

formasfunction

2:20 pm on Oct 2, 2007 (gmt 0)

10+ Year Member



Whats the best way to download those favicon images to my server? Is curl the way to go?

joelgreen

2:46 pm on Oct 2, 2007 (gmt 0)

10+ Year Member



For your current solution you could limit respond time by setting a timeout. Look at the fsockopen manual on the php.net for details.

But as previously stated storing favicons on your server is much more stable solution. You could use fread/fwrite for loading/saving the image. You could also use file_get_contents/file_put_contents. But file_put_contents was introduced in php5 only.

Achernar

5:09 pm on Oct 2, 2007 (gmt 0)

10+ Year Member Top Contributors Of The Month



Why not use a browser-side solution?

<img src="http://www.example.com/favicon.ico" onerror="this.src='/images/favicondefault.gif'">

joelgreen

9:39 am on Oct 3, 2007 (gmt 0)

10+ Year Member



Very smart solution Achernar!

formasfunction

2:30 pm on Oct 3, 2007 (gmt 0)

10+ Year Member



Hey Achernar, that's a great solution and one that I had avoided because Safari doesn't recognize onerror. However, I just tested it with the Safari 3 beta and it seems it's working now and that's good enough for me. Thanks again.