Solved

Detecting google in the referer

Posted on 2009-05-17
4
401 Views
Last Modified: 2012-05-07
I have some code that grabs the referering pages referer url and parses it to detect whether it has the string google in it. However it doesnt seem to detect it. Can some look at my code and give a suggestion on how to make it better.


$referer = $_SERVER['HTTP_REFERER'];
 
  //Did they get here from a search?
  if((preg_match('/www\.google.*/i',$referer) && !preg_match('/^http:\/\/www\.google\.com\//i', $referer))
     || preg_match('/search\.atomz.*/i',$referer)
     || preg_match('/search\.msn.*/i',$referer)
     || preg_match('/search\.yahoo.*/i',$referer)
     || preg_match('/msxml\.excite\.com/i', $referer)
     || preg_match('/search\.lycos\.com/i', $referer)
     || preg_match('/www\.alltheweb\.com/i', $referer)
     || preg_match('/search\.aol\.com/i', $referer)
     || preg_match('/search\.iwon\.com/i', $referer)
     || preg_match('/ask\.com/i', $referer)
     || preg_match('/search\.cometsystems\.com/i', $referer)
     || preg_match('/www\.hotbot\.com/i', $referer)
     || preg_match('/www\.overture\.com/i', $referer)
     || preg_match('/www\.metacrawler\.com/i', $referer)
     || preg_match('/search\.netscape\.com/i', $referer)
     || preg_match('/www\.looksmart\.com/i', $referer)
     || preg_match('/go\.google\.com/i', $referer)
     || preg_match('/dpxml\.webcrawler\.com/i', $referer)
     || preg_match('/search\.earthlink\.net/i', $referer)
     || preg_match('/search\.viewpoint\.com/i', $referer)
     || preg_match('/www\.mamma\.com/i', $referer)
     || preg_match('/home\.bellsouth\.net\/s\/s\.dll/i', $referer)
     || preg_match('/www\.ask\.co\.uk/i', $referer)) {
 
Echo "Search Engine Detected";
 
}
 
Here is the value of the referer varibale.
 
http://www.google.com/search?client=safari&rls=en-us&q=Danbury+CT+chive&ie=UTF-8&oe=UTF-8

Open in new window

0
Comment
Question by:MayoorPatel
  • 3
4 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 24407843
Make a function with an array of the things you want to match.  I will post an example in  a few moments. ~Ray
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 24407966
HTH, ~Ray
<?php // RAY_check_refer.php
error_reporting(E_ALL);
 
// REQUIRED READING: http://en.wikipedia.org/wiki/List_of_search_engines
 
// FOR THIS TEST CASE EXAMPLE ONLY WE SET THE REFERER TO A KNOWN VALUE
// $_SERVER["HTTP_REFERER"] = 'http://www.google.com/search?client=safari&rls=en-us&q=Danbury+CT+chive&ie=UTF-8&oe=UTF-8';
 
 
 
 
// TEST THE REFERER - WILL THROW NOTICE IF UNDEFINED
if (is_search_engine())  echo "    SEARCH ENGINE: {$_SERVER["HTTP_REFERER"]}";
if (!is_search_engine()) echo "NOT SEARCH ENGINE: {$_SERVER["HTTP_REFERER"]}";
 
 
 
// FUNCTION TO CHECK FOR SEARCH ENGINE REFERRAL
function is_search_engine()
{
// IF NOT REFERED
    if (empty($_SERVER["HTTP_REFERER"])) return FALSE;
 
// A LIST OF SELECTED SEARCH ENGINES
    $r = array();
    $r[] = '/google\.*/i';
    $r[] = '/search\.atomz.*/i';
    $r[] = '/search\.msn.*/i';
    $r[] = '/search\.yahoo.*/i';
    $r[] = '/msxml\.excite\.com/i';
    $r[] = '/search\.lycos\.com/i';
    $r[] = '/www\.alltheweb\.com/i';
    $r[] = '/search\.aol\.com/i';
    $r[] = '/search\.iwon\.com/i';
    $r[] = '/ask\.com/i';
    $r[] = '/search\.cometsystems\.com/i';
    $r[] = '/www\.hotbot\.com/i';
    $r[] = '/www\.overture\.com/i';
    $r[] = '/www\.metacrawler\.com/i';
    $r[] = '/search\.netscape\.com/i';
    $r[] = '/www\.looksmart\.com/i';
    $r[] = '/go\.google\.com/i';
    $r[] = '/dpxml\.webcrawler\.com/i';
    $r[] = '/search\.earthlink\.net/i';
    $r[] = '/search\.viewpoint\.com/i';
    $r[] = '/www\.mamma\.com/i';
    $r[] = '/home\.bellsouth\.net\/s\/s\.dll/i';
    $r[] = '/www\.ask\.co\.uk/i';
 
// TEST IF THIS IS A SEARCH ENGINE
    foreach ($r as $regex_string)
    {
        if (preg_match($regex_string, $_SERVER["HTTP_REFERER"])) return TRUE;
    }
 
// NOT A SEARCH ENGINE
    return FALSE;
}

Open in new window

0
 
LVL 1

Author Closing Comment

by:MayoorPatel
ID: 31582435
Excellent
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 25144991
Thanks for the points!  It's a good question, ~Ray
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
array_values - reorder after unset? 5 20
UPDATE query not working in mysqli php 8 48
mysqli insert query problems 4 21
does post require a form or curl to be post 4 29
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

813 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

9 Experts available now in Live!

Get 1:1 Help Now