We help IT Professionals succeed at work.
Get Started

Search Engine Referrer & Keyword PHP Script Problem

igotdreams
igotdreams asked
on
1,411 Views
Last Modified: 2013-12-07
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>';
}
?>
Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 9 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE