I made a lot of changes recently including a fix. Now it will also work with DogPile and fixed some more bugs on it. If you know of Referers it does not nail down please send them over in the comment form and I'll add them to the site!
The search engine refering query is the text users searched for and found your site. Look at the image below for example :

You can see how I searched for search engine refering query in Google and found this page.
You will notice many sites show you the query you used in google or other search engines to find them and sometimes mark the search terms in their content with colors.
By knowing what query was used in search engines to find you you can do several other things. You can guide visitors to further content based on their searches, you can record queries and have a simple status of your own for the website.
This function translates searches from Google, Yahoo, MSN, Ask, AOL, Digg and others that have the same query format.
The function will deliver an array containing several fields as explained below.
<?
// ----------------------------------------------------------------------------
/* $ref is optional, if not set will use current! */
function seReferer($ref = false){
$SeReferer = (is_string($ref) ? $ref : $_SERVER['HTTP_REFERER']);
if( //Check against Google, Yahoo, MSN, Ask and others
preg_match(
"/[&\?](q|p|w|searchfor|as_q|as_epq|s|query)=([^&]+)/i",
$SeReferer,$pcs)
){
if(preg_match("/https?:\/\/([^\/]+)\//i",$SeReferer,$SeDomain)){
$SeDomain = trim(strtolower($SeDomain[1]));
$SeQuery = $pcs[2];
if(preg_match("/[&\?](start|b|first|stq)=([0-9]*)/i",$SeReferer,$pcs)){
$SePos = (int)trim($pcs[2]);
}
}
}
if(!isset($SeQuery)){
if( //Check against DogPile
preg_match(
"/\/search\/web\/([^\/]+)\//i",
$SeReferer,$pcs)
){
if(preg_match("/https?:\/\/([^\/]+)\//i",$SeReferer,$SeDomain)){
$SeDomain = trim(strtolower($SeDomain[1]));
$SeQuery = $pcs[1];
}
}
}
// We Do Not have a query
if(!isset($SeQuery)){ return false; }
$OldQ=$SeQuery;
$SeQuery=urldecode($SeQuery);
// The Multiple URLDecode Trick to fix DogPile %XXXX Encodes
while($SeQuery != $OldQ){
$OldQ=$SeQuery; $SeQuery=urldecode($SeQuery);
}
//-- We have a query
return array(
"Se"=>$SeDomain,
"Query"=>$SeQuery,
"Pos"=>(int)$SePos,
"Referer"=>$SeReferer
);
}
// ----------------------------------------------------------------------------
?>
The function compares the referer to a list of known search query strings: //Google.com referer URL sample: As you see the user's query is container in the q parameter of the referer query string. Now look at the regular expression used :
http://www.google.com/search?hl=en&q=referer+search+engine+query
//Yahoo.com referer URL sample:
http://search.yahoo.com/search?p=refering+search+engine+query
//The Known variable names used by search engines:
q|p|w|searchfor|as_q|as_epq|query
Try this to see the results after you visit your pages by doing a site:your-site.com search in Google. Use this PHP code: $se=seReferer(); print_r($se);
Post Feedback