Selective Google Adsense and Firefox Ads
I’m still running my AdSense experiment (update: experiment over), and even the meagre earnings so far have produced some interesting results. I’ve been only showing ads to visitors arriving from search engines, then a few days back tweaked the code to show ads for Firefox with the Google Toolbar to those using Internet Explorer for Windows. Yeah, I know, I’m cruelly harassing poor IE users, but the idea intrigued me.
Here’s the simple PHP used to selectively display the ads, in case anyone finds it useful:
Show ads only to search engine referrals
<?
if (preg_match('/^http:\/\/(\w+\.)?(google|msn|yahoo|aol)\./',$_SERVER['HTTP_REFERER']) == 1)
{
?>
<!-- Google AdSense HTML -->
<?
}
?>
The expression used doesn’t bother to fully check the referring URL to see if it’s a known search results page, it’s enough to look for domains that appear to be part of Google, MSN, Yahoo! or AOL.
Show ad only to Windows Internet Explorer users
<?
$userAgent = $_SERVER['HTTP_USER_AGENT'];
if ( (strpos($userAgent,'Windows') !== false) && (strpos($userAgent,'MSIE') !== false) && (strpos($userAgent,'Opera') === false) )
{
?>
<!-- Firefox ad HTML -->
<?
}
?>
Browsers with ‘Windows’ and ‘MSIE’ in their descriptions are our targets, but there’s an extra check to make sure Opera users aren’t nagged.
Both combined
The code I’m currently using only displays ads for search engine referrals, showing Firefox ads to IE users and AdSense to everyone else:
<?
if (preg_match('/^http:\/\/(\w+\.)?(google|msn|yahoo|aol)\./',$_SERVER['HTTP_REFERER']) == 1)
{
$userAgent = $_SERVER['HTTP_USER_AGENT'];
if ( (strpos($userAgent,'Windows') !== false) && (strpos($userAgent,'MSIE') !== false) && (strpos($userAgent,'Opera') === false) )
{
?>
<!-- Firefox ad HTML -->
<?
}
else
{
?>
<!-- Google AdSense HTML -->
<?
}
}
?>
Tue 7th Feb 2006, 10:03pm GMT (updated Mon 13th Mar 2006, 9:42pm GMT)
Filed under: Hints and Tips, Marketing and Advertising, Search Engines, Server-side Coding, Web
Comments
Hmm sounds odd. Got JavaScript disabled, or some kind of ad-blocking extension installed?
Does your browser definitely send a referrer? (Can check using http://www.proxyway.com/cgi-bin/Ch… )The ads certainly aren't subtle!
Comments are now closed for this entry.
Matt Round’s company blog, covering web development, media, technology and pretty much anything else.
- Web Sites
- Good-looking, effective, accessible sites.
- Multimedia
- Logos, Flash games, animation and illustration.
- Advice
- Help with strategy, planning and getting noticed.

— morcs, 9th Feb, 8:02pm