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=(.*?)\&#si', $this->referer, $this->keys);
$this->search_engine = 'Google';
}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';
}
return array($this->referer,(!is_array($this->keys) ? $this->keys : $this->keys[1]),$this->search_engine);
}
return array();
}
} ?>
My code on the page to report the information;
<?php
require_once('lib/class/searchkeys.class.php');
$keys =& new search_keywords();
$keys = $keys->get_keys();
if (count($keys)){
print '<p align="center">Directed to this page from <strong>'.$keys[2].'</strong> search engine, within Keywords <strong>'.$keys[1].'</strong></p>';
}else{
print '<p align="center">nope</p>';
}
?>