Solved

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

Posted on 2010-09-03
4
459 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 82

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 108

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now