Hi!
I'd like to show the ads on my page only to visitors coming from search engines. I'm looking for a php script that can do the trick - if lucky and I'll find it, I'll integrate it in my page.
If you know of such thing or how to do it, please post it here, I'll highly apreciate it.
Thank you!

Hi wattaman,

You can use the code below, but you will need to add more search engines after the 8th line.

<?php
$refurl = $_SERVER['HTTP_REFERER']; 
$refurl = explode("/", $refurl); 
$refurl = $refurl[0]."//".$refurl[2];
if ($refurl == www.google.com) echo "Add code here!";
if ($refurl == www.google.co.uk) echo "Add code here!";
if ($refurl == www.bing.com) echo "Add code here!";
?>

Calum

Thanks, Calum, but is not working. No ads (or whatever I insert there) is displayed.
Have no idea why :(

What version of google are you using e.g. google.fr / es / co.uk ect if not .co.uk or com you will need to edit it, also some browsers don't send the refer header, try wit a different one.
Is your site in google? because it doesn't work by just going to google and typing the url in the address bar.

hope this works

Calum

Nothing works, Calum. The site is in Google and I've checked in IE, FF3 and Chrome browsers. Is it possible to be something wrong with my server?

Also, I'm trying this javascript without succes, too:

<script type="text/javascript">
var ref = document.referrer;
//you can add more URLs to not show ads, for example you want to block Google and Yahoo images traffic but show to any other Google and Yahoo traffic in 
var se = new Array('images.google.','images.yahoo.');
//you can add more search engines below, just follow the same format!
var se2 = new Array('.google.','.yahoo.','.bing.','.ask.','.msn.');
for (var i = 0; i <= se.length-1; i++) {
        if (ref.indexOf(se[i])!== -1) {
                var expiry = new Date ();
                expiry.setTime(expiry.getTime() + 60000);
                document.cookie = "sevisitor=1; expires=" + expiry.toGMTString() + "; path=/; ";
        }
}
</script>
<script type="text/javascript">
var results = document.cookie.match ( '(^|;) ?sevisitor=([^;]*)(;|$)' );

if(results) {
 if (results[2] == 1) {
//if traffic from images.google.com or images.yahoo.com do nothing
//or you can show CPM ads or whatever you want to do.
 }
}else {

   var results2 = document.cookie.match ( '(^|;) ?sevisitor2=([^;]*)(;|$)' );
    if(results2) {
    if (results2[2] == 1) {
  //if traffic coming from google.com ,yahoo.com ,msn.com, bing.com, or ask.com
  //IMPORTANT: replace pub-xxxxxxxxxxxxx with your publisher id!
 //   Also replace yyyyyyyy with your google_ad_slot

var gstring="<script type=\"text/javascript\"><!--\n";
gstring+="google_ad_client = \"pub-xxxxxxxxxxxxxxxx\"\n";
gstring+="google_ad_slot = \"yyyyyyyyyyy\";\n";
gstring+="google_ad_width = 336;\n";
gstring+="google_ad_height = 280;\n";
gstring+="//-->\n";
gstring+="<\/script>\n";
gstring+="<script type=\"text/javascript\"\n";
gstring+="src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\">\n";
gstring+="<\/script>\n";
document.write(gstring);
     }
     }
}
</script>

Borrowed from here.

You may want to redirect people coming from like www.google.com to another page that has the ads using htaccess... just a thought.. maybe a bit painful to implement

You may want to redirect people coming from like www.google.com to another page that has the ads using htaccess... just a thought.. maybe a bit painful to implement

Can do, but I want the same page for everyone, only to hide ads from time to time (or show to some visitors only). Thanks for the suggestion.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.