Solved

PHP: How can I detect if a user came from a Search Engine

Posted on 2010-09-03
4
466 Views
Last Modified: 2012-05-10
I have a site where I award user's Viz (currency) for giving users their referral url and when a person being referred clicks it, they person referring them gets the Viz.  However, a problem is that Google has indexed some of those url's with the referral code in them so what I want to do is detect if the referred user is coming from a Search engine and not award the Viz.
0
Comment
Question by:davideo7
4 Comments
 
LVL 2

Assisted Solution

by:Gatherer_Hade
Gatherer_Hade earned 100 total points
ID: 33598752
Check the contents of the variable
$_SERVER['HTTP_REFERER']

You can use strpos() to look for google, yahoo, bing, etc within this variable.
0
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 100 total points
ID: 33598787
Look at the HTTP_REFERER string, "$_SERVER['HTTP_REFERER'];" and User Agent string, "$_SERVER['HTTP_USER_AGENT'];".  There are a lot of search engines out there.  You probably want to focus on the 'big' ones like Google, Bing, Yahoo, and Baidu.
0
 
LVL 12

Assisted Solution

by:GMGenius
GMGenius earned 100 total points
ID: 33598799
The problem you can get using the HTTP_REFERER is that some antivus software will block the value from the browser, the HTTP_REFERER is client side.
Your best option is to pick up a passed variable ?ref=12345 in the url and pull it in your PHP code
eg
$refer = @$_GET["ref"];
but this will require the URL to contact the correct referal code
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 200 total points
ID: 33602873
This is an old piece of code (I would not use EREG today) but it demonstrates how I tested the user agent.  I used this to kill script outputs when I did not want the pages indexed by some of the 'bots.  Maybe a similar adaptation will work for you.

You might want to read about this:
http://www.robotstxt.org/

And this...
http://en.wikipedia.org/wiki/List_of_search_engines#Based_on

An interesting exercise might be to capture REQUEST_URI, HTTP_REFERER and HTTP_USER_AGENT fields and write these to a data base (or mail them to yourself).  You will see the patterns developing very quickly, I'm sure.  

Regards, ~Ray

<?php
function bad_robots() 
{
    //
    // DENY SOME SCRIPTS TO ROBOTS
    //
    $bad_robots[]='crawler';
    $bad_robots[]='spider';
    $bad_robots[]='robot';
    $bad_robots[]='slurp';
    $bad_robots[]='Atomz';
    $bad_robots[]='googlebot';
    $bad_robots[]='VoilaBot';
    $bad_robots[]='msnbot';
    $bad_robots[]='Gaisbot';
    $bad_robots[]='Gigabot';
    $bad_robots[]='SBIder';
    $bad_robots[]='Zyborg';
    $bad_robots[]='FunWebProducts';
    $bad_robots[]='findlinks';
    $bad_robots[]='ia_archiver';
    $bad_robots[]='MJ12bot';
    $bad_robots[]='Ask Jeeves';
    $bad_robots[]='NG/2.0';
    $bad_robots[]='voyager';
    $bad_robots[]='Exabot';
    $bad_robots[]='Nutch';
    $bad_robots[]='Hercules';
    $bad_robots[]='psbot';
    $bad_robots[]='LocalcomBot';

    // GET THE AGENT
    $agt = $_SERVER["HTTP_USER_AGENT"];
    $bad = FALSE;
    
    // TEST IF AGENT IN LIST
    foreach ($bad_robots as $spider) 
    {
        if (eregi("$spider", $agt)) { $bad++; }
    }
    
    // AGENT FOUND?
    if ($bad) return true;
    return false; 
}

Open in new window

0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question