<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="https://shkspr.mobi/blog/wp-content/themes/edent-wordpress-theme/rss-style.css?cache=2026-11-13T15:19:59+00:00" type="text/css"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	    xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	     xmlns:dc="http://purl.org/dc/elements/1.1/"
	   xmlns:atom="http://www.w3.org/2005/Atom"
	     xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	  xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>
<html:meta xmlns:html="http://www.w3.org/1999/xhtml" name="viewport" content="width=device-width" />
<channel>
	<title>favicon &#8211; Terence Eden’s Blog</title>
	<atom:link href="https://shkspr.mobi/blog/tag/favicon/feed/" rel="self" type="application/rss+xml" />
	<link>https://shkspr.mobi/blog</link>
	<description>Regular nonsense about tech and its effects 🙃</description>
	<lastBuildDate>Sun, 21 Sep 2025 20:20:57 +0000</lastBuildDate>
	<language>en-GB</language>
	<copyright>© Terence Eden. 🄯 CC BY-SA. See https://shkspr.mobi/blog/copyright-and-copyleft/</copyright>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.4</generator>

<image>
	<url>https://shkspr.mobi/blog/wp-content/uploads/2023/07/cropped-avatar-32x32.jpeg</url>
	<title>favicon &#8211; Terence Eden’s Blog</title>
	<link>https://shkspr.mobi/blog</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title><![CDATA[A Self-Hosted Favicon Proxy written in PHP]]></title>
		<link>https://shkspr.mobi/blog/2025/10/a-self-hosted-favicon-proxy-written-in-php/</link>
					<comments>https://shkspr.mobi/blog/2025/10/a-self-hosted-favicon-proxy-written-in-php/#comments</comments>
				<dc:creator><![CDATA[Terence Eden]]></dc:creator>
		<pubDate>Tue, 28 Oct 2025 12:34:54 +0000</pubDate>
		<category><![CDATA[favicon]]></category><category><![CDATA[HowTo]]></category><category><![CDATA[HTML]]></category><category><![CDATA[PHP]]></category>		<guid isPermaLink="false">https://shkspr.mobi/blog/?p=63434</guid>

		
		<description>In theory, you should be able to get the base favicon of any domain by calling /favicon.ico - but the reality is somewhat more complex than that. Plenty of sites use a wide variety of semi-standardised images which are usually only discoverable from the site's HTML.  There are several services which allow you to get favicons based on a domain. But they all have their problems.   Google   Exposes…</description>
			
		<content:encoded>&lt;p&gt;In theory, you should be able to get the base favicon of any domain by calling &lt;code&gt;/favicon.ico&lt;/code&gt; - but the reality is somewhat more complex than that. Plenty of sites use a wide variety of semi-standardised images which are usually only discoverable from the site's HTML.&lt;/p&gt;

&lt;p&gt;There are several services which allow you to get favicons based on a domain. But they all have their problems.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.google.com/s2/favicons?domain=shkspr.mobi&amp;amp;sz=256"&gt;Google&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;Exposes your user's to Google's tracking.&lt;/li&gt;
&lt;li&gt;Relies on redirects.&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://icons.duckduckgo.com/ip9/shkspr.mobi.ico"&gt;DuckDuckGo&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;Not officially supported by DDG.&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://favicon.is/shkspr.mobi"&gt;Favicon.is&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;No privacy policy whatsoever.&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://icon.horse/"&gt;Icons.horse&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;Paid service.&lt;/li&gt;
&lt;li&gt;Only small size icons.&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://favicone.com/shkspr.mobi"&gt;Favicone&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;No privacy policy.&lt;/li&gt;
&lt;li&gt;Only small size icons.&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I want to show favicons next to specific links, but I don't want to expose my visitors to unnecessary tracking. How can I proxy these images so they are stored and served locally?&lt;/p&gt;

&lt;p&gt;There are a few existing services. Some use &lt;a href="https://github.com/seadfeng/favicons-proxy"&gt;Cloudflare workers&lt;/a&gt; or other &lt;a href="https://github.com/shaklain125/gicon"&gt;cloud services&lt;/a&gt;, there are some local-first ones which are &lt;a href="https://github.com/toolness/favicon-proxy"&gt;unmaintained&lt;/a&gt;.  But nothing modern, self-hosted, and as easy to deploy as uploading a single PHP file.&lt;/p&gt;

&lt;p&gt;So here's my attempt to make something which will preserve user privacy, be reasonably fast, and have moderately up-to-date icons, while remaining fast and efficient.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;&lt;nav role="doc-toc"&gt;&lt;menu&gt;&lt;li&gt;&lt;h2 id="table-of-contents"&gt;&lt;a href="https://shkspr.mobi/blog/2025/10/a-self-hosted-favicon-proxy-written-in-php/#table-of-contents"&gt;Table of Contents&lt;/a&gt;&lt;/h2&gt;&lt;menu&gt;&lt;li&gt;&lt;a href="https://shkspr.mobi/blog/2025/10/a-self-hosted-favicon-proxy-written-in-php/#getting-the-domain"&gt;Getting the domain&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="https://shkspr.mobi/blog/2025/10/a-self-hosted-favicon-proxy-written-in-php/#getting-the-image"&gt;Getting the image&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="https://shkspr.mobi/blog/2025/10/a-self-hosted-favicon-proxy-written-in-php/#getting-the-structure-right"&gt;Getting the structure right&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="https://shkspr.mobi/blog/2025/10/a-self-hosted-favicon-proxy-written-in-php/#preventing-abuse"&gt;Preventing abuse&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="https://shkspr.mobi/blog/2025/10/a-self-hosted-favicon-proxy-written-in-php/#putting-it-all-together"&gt;Putting it all together&lt;/a&gt;&lt;/li&gt;&lt;/menu&gt;&lt;/li&gt;&lt;/menu&gt;&lt;/nav&gt;&lt;p&gt;&lt;/p&gt;

&lt;h2 id="getting-the-domain"&gt;&lt;a href="https://shkspr.mobi/blog/2025/10/a-self-hosted-favicon-proxy-written-in-php/#getting-the-domain"&gt;Getting the domain&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;Assuming the request comes in to &lt;code&gt;https://proxy.example.com/?domain=bbc.co.uk&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;PHP has a &lt;a href="https://www.php.net/manual/en/filter.constants.php#constant.filter-validate-domain"&gt;handy &lt;code&gt;FILTER_VALIDATE_DOMAIN&lt;/code&gt; filter&lt;/a&gt; which will determine if the string is a domain.&lt;/p&gt;

&lt;pre&gt;&lt;code class="language-php"&gt;filter_var( $domain, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME );
&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id="dealing-with-idns"&gt;&lt;a href="https://shkspr.mobi/blog/2025/10/a-self-hosted-favicon-proxy-written-in-php/#dealing-with-idns"&gt;Dealing with IDNs&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Some domains contain non-ASCII characters - for example &lt;a href="https://莎士比亚.org/"&gt;https://莎士比亚.org/&lt;/a&gt; - not all favicon services support International Domain Names.&lt;/p&gt;

&lt;p&gt;Using &lt;a href="https://www.php.net/manual/en/function.idn-to-ascii.php"&gt;the &lt;code&gt;idn_to_ascii()&lt;/code&gt; function&lt;/a&gt;, it is possible to get the Punycode domain.&lt;/p&gt;

&lt;pre&gt;&lt;code class="language-php"&gt;$domain = idn_to_ascii("莎士比亚.org");
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id="getting-the-image"&gt;&lt;a href="https://shkspr.mobi/blog/2025/10/a-self-hosted-favicon-proxy-written-in-php/#getting-the-image"&gt;Getting the image&lt;/a&gt;&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Check if the icon has previously been downloaded.&lt;/li&gt;
&lt;li&gt;Rotate randomly between a few different Favicon services.&lt;/li&gt;
&lt;li&gt;Download the icon.&lt;/li&gt;
&lt;li&gt;Save it somewhere.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id="getting-the-structure-right"&gt;&lt;a href="https://shkspr.mobi/blog/2025/10/a-self-hosted-favicon-proxy-written-in-php/#getting-the-structure-right"&gt;Getting the structure right&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;I know from my work on OpenBenches that storing tens of thousands of files in a single directory can be problematic. So I'll store the retrieved favicon in: &lt;code&gt;/tld/domain/subdomain/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;That will make it quick to see if an icon exists. I'll save the file with a filename based on the current timestamp. That will allow me to check if an icon is out of date, and will prevent people downloading the icons directly from me.&lt;/p&gt;

&lt;h2 id="preventing-abuse"&gt;&lt;a href="https://shkspr.mobi/blog/2025/10/a-self-hosted-favicon-proxy-written-in-php/#preventing-abuse"&gt;Preventing abuse&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;I don't want anyone but visitors to my site to be able to use this service. So I'll add a (weak) check to see if the request came from my domain.&lt;/p&gt;

&lt;pre&gt;&lt;code class="language-php"&gt;$referer = parse_url( $_SERVER["HTTP_REFERER"], PHP_URL_HOST );
if ( $referer == "shkspr.mobi") {
   …
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Some browsers may not send referers for privacy reasons. So they won't see the favicons. But they probably wouldn't have seen the images loaded from a 3&lt;sup&gt;rd&lt;/sup&gt; party service. So I'll serve a default image.&lt;/p&gt;

&lt;h2 id="putting-it-all-together"&gt;&lt;a href="https://shkspr.mobi/blog/2025/10/a-self-hosted-favicon-proxy-written-in-php/#putting-it-all-together"&gt;Putting it all together&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;You can grab the code from &lt;a href="https://git.edent.tel/edent/Favicon-Proxy-PHP"&gt;my personal git service&lt;/a&gt;.&lt;/p&gt;
&lt;img src="https://shkspr.mobi/blog/wp-content/themes/edent-wordpress-theme/info/okgo.php?ID=63434&amp;amp;HTTP_REFERER=RSS" alt="" width="1" height="1" loading="eager"&gt;</content:encoded>
	

					<wfw:commentRss>https://shkspr.mobi/blog/2025/10/a-self-hosted-favicon-proxy-written-in-php/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
	</channel>
</rss>
