Pressure is nothing more than the shadow of great opportunity. - Michael Johnson

Serving adverts conditionally based on referrer

9 years, 9 months ago

Recently a number of people have been discussing the idea of serving up Google adverts conditionally based on the referring site. By targeting referrals from search engines this gives them the opportunity to gain some extra revenue from what are likely one time visitors whilst keeping their sites advert free for regular visitors. To me this seems a sensible approach. I've written a quick function to facilitate this which detects if the user has visited the page from a search engine and returns true or false accordingly. To keep the regular expression line short I've only included Google and Yahoo but you can easily add others - just add them, pipe separated, between the rounded brackets in the regular expression.

  1. <?php
  2. function IsFromSearchEngine() {
  3. if (isset($_SERVER['HTTP_REFERER']) && preg_match("/^https?:\/\/[0-9a-z]*\.?(google|yahoo)\..+\/.*$/i", $_SERVER['HTTP_REFERER'])) {
  4. return true;
  5. }
  6. return false;
  7. }
  8. ?>

To use simply include this function and wrap the following code around your Google advert calls.

  1. <?php
  2. if (IsFromSearchEngine()) {
  3. echo 'Adverts';
  4. }
  5. ?>

Understanding The Regular Expression

The regular expression is fairly complete. There are more checks that could be made but I honestly don't think it's worth it for this problem.

  1. ^ - matches the start of the string.
  2. http - matches, well "http".
  3. s? - optionally matches a single letter "s" to cater for "http" and "https".
  4. : - matches a colon.
  5. \/\/ - matches two forward slashes. They need to be escaped with a backlash in the regular expression as they otherwise delimit the start and end of the expression.
  6. [0-9a-z]* - matches zero or more instances of the character ranges 0-9 and a-z.
  7. \.? - optionally matches a literal full stop (period).
  8. (google|yahoo) - matches the term "google" or the term "yahoo".
  9. \. - matches a literal full stop (period).
  10. .+ - matches one or more characters.
  11. \/ - matches a forward slash.
  12. .* - matches zero or more characters
  13. $ - matches the end of the string.

The "i" after the final forward slash is a modifier which makes the entire regular expression case insensitive.

Further Reading

Comments

  • Thanks, Ed!

    stavrosthewonderchicken - 13th June 2007 #

  • Unless the advertising is something obtrusive and annoying I don't see why you'd remove it for regulars. Whenever I browse around I'm not annoyed by advertising unless it's doing something overly obtrusive to catch my attention. So IMO it seems like a waste of CPU cycles.

    Also you're error log is going to be filled with uninitialized variable $_SERVER['HTTP_REFERER'] pretty quikcly. :)

    Martin Fjordvald - 14th June 2007 #

  • Martin - well spotted, thanks. I've updated the code accordingly.

    Ed Eliot - 14th June 2007 #

Help make this post better

Notes: Standard BBCode for links, bold, italic and code are supported. rel="nofollow" is added to all links. Your email address will never be displayed on the site.

Back to index