I am trying to write a PHP script that collects website data.
I am trying to write a function that checks if the referrer was a search engine, if it is it gets the search engine domain and the keywords used.
Any tips?
I am trying to write a PHP script that collects website data.
I am trying to write a function that checks if the referrer was a search engine, if it is it gets the search engine domain and the keywords used.
Any tips?
You'll need to use $_SERVER['HTTP_REFERER'] to determine that information. There is also a PHP class you can use or take ideas from located here: CReferrer (referer) - PHP Classes
Here is some of my code, does not seem to work though!
Any ideas?
Code:function searchEngine()
{
$refer = parse_url($_SERVER['HTTP_REFERER']);
$host = $refer['host'];
if(strstr($host,'google'))
{
$searchengine = "yes";
$sedomain = "Google";
return $searchengine;
}
elseif(strstr($host,'yahoo'))
{
$searchengine = "yes";
$sedomain = "Yahoo";
return $sedomain;
}
elseif(strstr($host,'msn'))
{
$searchengine = "yes";
$sedomain = "MSN";
return $sedomain;
}
}
function getKeywords()
{
$refer = parse_url($_SERVER['HTTP_REFERER']);
$host = $refer['host'];
$refer = $refer['query'];
if(strstr($host,'google'))
{
//do google stuff
$match = preg_match('/&q=([a-zA-Z0-9+-]+)/',$refer, $output);
$querystring = $output[0];
$querystring = str_replace('&q=','',$querystring);
$keywords = explode('+',$querystring);
return $keywords;
}
elseif(strstr($host,'yahoo'))
{
//do yahoo stuff
$match = preg_match('/p=([a-zA-Z0-9+-]+)/',$refer, $output);
$querystring = $output[0];
$querystring = str_replace('p=','',$querystring);
$keywords = explode('+',$querystring);
return $keywords;
}
elseif(strstr($host,'msn'))
{
//do msn stuff
$match = preg_match('/q=([a-zA-Z0-9+-]+)/',$refer, $output);
$querystring = $output[0];
$querystring = str_replace('q=','',$querystring);
$keywords = explode('+',$querystring);
return $keywords;
}
else
{
//else, who cares
return false;
}
}
The:
$keywords & $sedomain
variables seem to be returned blank!
How do I return more that one variable from a function?
Anyone got any advice on this!
Really want to create a good script to collect web stats.
Anyone know any existing scripts that i could customize?
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks
Algorithms and Data Structures
Java tutorials
Algorithms Forum