igotdreams
asked on
Search Engine Referrer & Keyword PHP Script Problem
I have a slight problem with a php class that is intended to determine the visiting search engine and keywords used then display them on the page. I get the script to properly report the referrer but not the keyword.
Code below and attached.
The Php Class;
<?php
class search_keywords{
var $referer;
var $search_engine;
var $keys;
var $sep;
function search_keywords(){
$this->referer = '';
$this->sep = '';
if ($_SERVER['HTTP_REFERER'] OR $_ENV['HTTP_REFERER']){
$this->referer = urldecode(($_SERVER['HTTP_ REFERER'] ? $_SERVER['HTTP_REFERER'] : $_ENV['HTTP_REFERER']));
$this->sep = (eregi('(\?q=|\?qt=|\?p=)' , $this->referer)) ? '\?' : '\&';
}
}
function get_keys(){
if (!empty($this->referer)){
if (eregi('www\.google', $this->referer)){
// Google
preg_match('#{$this->sep}q =(.*?)\&#s i', $this->referer, $this->keys);
$this->search_engine = 'Google';
}else if (eregi('(yahoo\.com|search \.yahoo)', $this->referer)){
// Yahoo
preg_match('#{$this->sep}p =(.*?)\&#s i', $this->referer, $this->keys);
$this->search_engine = 'Yahoo';
}else if (eregi('search\.msn', $this->referer)){
// MSN
preg_match('#{$this->sep}q =(.*?)\&#s i', $this->referer, $this->keys);
$this->search_engine = 'MSN';
}else if (eregi('www\.bing', $this->referer)){
// Bing
preg_match('#{$this->sep}q =(.*?)\&#s i', $this->referer, $this->keys);
$this->search_engine = 'Bing';
}else if (eregi('search\.aol', $this->referer)){
// AOL
preg_match('#{$this->sep}q =(.*?)\&#s i', $this->referer, $this->keys);
$this->search_engine = 'AOL';
}else if (eregi('www\.alltheweb', $this->referer)){
// AllTheWeb
preg_match('#{$this->sep}q =(.*?)\&#s i', $this->referer, $this->keys);
$this->search_engine = 'AllTheWeb';
}else if (eregi('(looksmart\.com|se arch\.look smart)', $this->referer)){
// Looksmart
preg_match('#{$this->sep}q t=(.*?)\&# si', $this->referer, $this->keys);
$this->search_engine = 'Looksmart';
}else if (eregi('(askjeeves\.com|as k\.com)', $this->referer)){
// AskJeeves
preg_match('#{$this->sep}q =(.*?)\&#s i', $this->referer, $this->keys);
$this->search_engine = 'AskJeeves';
}else{
$this->keys = 'Not available';
$this->search_engine = 'Unknown';
}
return array($this->referer,(!is_ array($thi s->keys) ? $this->keys : $this->keys[1]),$this->sea rch_engine );
}
return array();
}
} ?>
My code on the page to report the information;
<?php
require_once('lib/class/se archkeys.c lass.php') ;
$keys =& new search_keywords();
$keys = $keys->get_keys();
if (count($keys)){
print '<p align="center">Directed to this page from <strong>'.$keys[2].'</stro ng> search engine, within Keywords <strong>'.$keys[1].'</stro ng></p>';
}else{
print '<p align="center">nope</p>';
}
?>
Code below and attached.
The Php Class;
<?php
class search_keywords{
var $referer;
var $search_engine;
var $keys;
var $sep;
function search_keywords(){
$this->referer = '';
$this->sep = '';
if ($_SERVER['HTTP_REFERER'] OR $_ENV['HTTP_REFERER']){
$this->referer = urldecode(($_SERVER['HTTP_
$this->sep = (eregi('(\?q=|\?qt=|\?p=)'
}
}
function get_keys(){
if (!empty($this->referer)){
if (eregi('www\.google', $this->referer)){
preg_match('#{$this->sep}q
$this->search_engine = 'Google';
}else if (eregi('(yahoo\.com|search
// Yahoo
preg_match('#{$this->sep}p
$this->search_engine = 'Yahoo';
}else if (eregi('search\.msn', $this->referer)){
// MSN
preg_match('#{$this->sep}q
$this->search_engine = 'MSN';
}else if (eregi('www\.bing', $this->referer)){
// Bing
preg_match('#{$this->sep}q
$this->search_engine = 'Bing';
}else if (eregi('search\.aol', $this->referer)){
// AOL
preg_match('#{$this->sep}q
$this->search_engine = 'AOL';
}else if (eregi('www\.alltheweb', $this->referer)){
// AllTheWeb
preg_match('#{$this->sep}q
$this->search_engine = 'AllTheWeb';
}else if (eregi('(looksmart\.com|se
// Looksmart
preg_match('#{$this->sep}q
$this->search_engine = 'Looksmart';
}else if (eregi('(askjeeves\.com|as
// AskJeeves
preg_match('#{$this->sep}q
$this->search_engine = 'AskJeeves';
}else{
$this->keys = 'Not available';
$this->search_engine = 'Unknown';
}
return array($this->referer,(!is_
}
return array();
}
} ?>
My code on the page to report the information;
<?php
require_once('lib/class/se
$keys =& new search_keywords();
$keys = $keys->get_keys();
if (count($keys)){
print '<p align="center">Directed to this page from <strong>'.$keys[2].'</stro
}else{
print '<p align="center">nope</p>';
}
?>
Err sorry, misread that. You are returning an array, first element is the referrer, second is the keys map.
So the keys are:
$ret = $keys->get_keys();
$map = $ret[1];
if (count($keys)){
print '<p align="center">Directed to this page from <strong>'.$ret[0].'</strong> search engine, within Keywords <strong>'.$map.'</strong></p>';
ASKER
This did not work, I got this;
Directed to this page from http://www.google.com/search?sourceid=navclient&ie=UTF-8&rlz=1T4GGLL_enUS300US300&q=website design search engine, within Keywords
'.$map.' did not return anything. Same problem as before.
Directed to this page from http://www.google.com/search?sourceid=navclient&ie=UTF-8&rlz=1T4GGLL_enUS300US300&q=website design search engine, within Keywords
'.$map.' did not return anything. Same problem as before.
Are you sure your regexes are right?
Try this example:
Try this example:
<?php
class search_keywords{
var $referer;
var $search_engine;
var $keys;
var $sep;
function search_keywords(){
$this->referer = '';
$this->sep = '';
if ($_SERVER['HTTP_REFERER'] OR $_ENV['HTTP_REFERER']){
$this->referer = urldecode(($_SERVER['HTTP_REFERER'] ? $_SERVER['HTTP_REFERER'] : $_ENV['HTTP_REFERER']));
$this->sep = (eregi('(\?q=|\?qt=|\?p=)', $this->referer)) ? '\?' : '\&';
}
}
function get_keys(){
$this->referer = "http://www.google.com/#hl=en&source=hp&q=find+text+to+search&aq=f&aqi=g10&oq=&fp=76098cdeeeb6268e";
if (!empty($this->referer)){
if (eregi('www\.google', $this->referer)){
// Google
// preg_match('#{$this->sep}q=(.*?)\&#si', $this->referer, $this->keys);
preg_match('/q=([^\&]*)/', $this->referer, $this->keys);
$this->search_engine = 'Google';
// echo "Keywords: ".$this->keys."<br />\n";
echo "Keywords: ".$this->keys[1]."<br />\n";
}else if (eregi('(yahoo\.com|search\.yahoo)', $this->referer)){
// Yahoo
preg_match('#{$this->sep}p=(.*?)\&#si', $this->referer, $this->keys);
$this->search_engine = 'Yahoo';
}else if (eregi('search\.msn', $this->referer)){
// MSN
preg_match('#{$this->sep}q=(.*?)\&#si', $this->referer, $this->keys);
$this->search_engine = 'MSN';
}else if (eregi('www\.bing', $this->referer)){
// Bing
preg_match('#{$this->sep}q=(.*?)\&#si', $this->referer, $this->keys);
$this->search_engine = 'Bing';
}else if (eregi('search\.aol', $this->referer)){
// AOL
preg_match('#{$this->sep}q=(.*?)\&#si', $this->referer, $this->keys);
$this->search_engine = 'AOL';
}else if (eregi('www\.alltheweb', $this->referer)){
// AllTheWeb
preg_match('#{$this->sep}q=(.*?)\&#si', $this->referer, $this->keys);
$this->search_engine = 'AllTheWeb';
}else if (eregi('(looksmart\.com|search\.looksmart)', $this->referer)){
// Looksmart
preg_match('#{$this->sep}qt=(.*?)\&#si', $this->referer, $this->keys);
$this->search_engine = 'Looksmart';
}else if (eregi('(askjeeves\.com|ask\.com)', $this->referer)){
// AskJeeves
preg_match('#{$this->sep}q=(.*?)\&#si', $this->referer, $this->keys);
$this->search_engine = 'AskJeeves';
}else{
$this->keys = 'Not available';
$this->search_engine = 'Unknown';
}
echo "is array ".is_array($this->keys)."<br />\n";
return array( $this->referer,
(!is_array($this->keys) ? $this->keys :
$this->keys[1]),
$this->search_engine);
}
echo "Returning empty array<br />\n";
return array();
}
} ?>
ASKER
I guess that is the problem, but I do not know how to fix it. This is not my code.
So you want a space separated list of all keywords from the search engine referrer?
Can you provide examples of each referrer URL?
I used a hard coded example from google above.
Can you provide examples of each referrer URL?
I used a hard coded example from google above.
These regexes work for a more of them.
I haven't tested them all.
I haven't tested them all.
<?php
class search_keywords{
var $referer;
var $search_engine;
var $keys;
var $sep;
function search_keywords(){
$this->referer = '';
$this->sep = '';
if ($_SERVER['HTTP_REFERER'] OR $_ENV['HTTP_REFERER']){
$this->referer = urldecode(($_SERVER['HTTP_REFERER'] ? $_SERVER['HTTP_REFERER'] : $_ENV['HTTP_REFERER']));
$this->sep = (eregi('(\?q=|\?qt=|\?p=)', $this->referer)) ? '\?' : '\&';
}
}
function get_keys(){
// examples...
// $this->referer = "http://www.google.com/#hl=en&source=hp&q=find+text+to+search&aq=f&aqi=g10&oq=&fp=76098cdeeeb6268e";
// $this->referer = "http://search.yahoo.com/search?p=text+to+search&toggle=1&cop=mss&ei=UTF-8&fr=yfp-t-701";
if (!empty($this->referer)){
if (eregi('www\.google', $this->referer)){
// Google
preg_match('/q=([^\&]*)/', $this->referer, $this->keys);
$this->search_engine = 'Google';
}else if (eregi('(yahoo\.com|search\.yahoo)', $this->referer)){
// Yahoo
preg_match('/p=(.*?)\&/', $this->referer, $this->keys);
$this->search_engine = 'Yahoo';
}else if (eregi('search\.msn', $this->referer)){
// MSN
preg_match('/q=(.*?)\&/', $this->referer, $this->keys);
$this->search_engine = 'MSN';
}else if (eregi('www\.bing', $this->referer)){
// Bing
preg_match('/q=(.*?)\&/', $this->referer, $this->keys);
$this->search_engine = 'Bing';
}else if (eregi('search\.aol', $this->referer)){
// AOL
preg_match('/q=(.*?)\&/', $this->referer, $this->keys);
$this->search_engine = 'AOL';
}else if (eregi('www\.alltheweb', $this->referer)){
// AllTheWeb
preg_match('/q=(.*?)\&/', $this->referer, $this->keys);
$this->search_engine = 'AllTheWeb';
}else if (eregi('(looksmart\.com|search\.looksmart)', $this->referer)){
// Looksmart
preg_match('/qt=(.*?)\&/', $this->referer, $this->keys);
$this->search_engine = 'Looksmart';
}else if (eregi('(askjeeves\.com|ask\.com)', $this->referer)){
// AskJeeves
preg_match('/q=(.*?)\&/', $this->referer, $this->keys);
$this->search_engine = 'AskJeeves';
}else{
$this->keys = 'Not available';
$this->search_engine = 'Unknown';
}
echo "is array ".is_array($this->keys)."<br />\n";
return array( $this->referer,
(!is_array($this->keys) ? $this->keys :
$this->keys[1]),
$this->search_engine);
}
echo "Returning empty array<br />\n";
return array();
}
} ?>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks
return array();
Shouldn't you
return this-keys;