Solved

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

Posted on 2010-09-03
4
463 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 109

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

773 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