title: How to display Google AdSense ads to search engine traffic only?
body: <p>I want to display Google AdSense ads to search engine traffic only and NO ads to regular visitors coming in directly or from Facebook, Twitter, email link, etc...</p>
  
  <p>Here's the code I currently use and it seems to work okay, but I want to also improve the code to include many other search engines in addition to Google such as Bing, Yahoo, Ask, etc.. Would someone mind looking at the code below and revise it with improvements?</p>
  
  <pre><code>&lt;?php
  $ref = $_SERVER['HTTP_REFERER'];
  if (preg_match("(google|yahoo|bing)", $ref) != false) {
  echo &lt;&lt;&lt;END
  &lt;script type="text/javascript"&gt;&lt;!--
  google_ad_client = "xx-xx-xxxxxxxxxxxxxxxxxx";
  /* xxxxxxxx xxxxxx xxx xxx xxx xx xxxxxx */
  google_ad_slot = "xxxxxxxxxxxxxx";
  google_ad_width = xxx;
  google_ad_height = xxx;
  //--&gt;
  &lt;/script&gt;
  &lt;script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js"&gt;
  &lt;/script&gt;
  &lt;/div&gt;
  END;
  }
  else {
       echo ""
  ;
  }
  ?&gt;
  </code></pre>