homepage Welcome to WebmasterWorld Guest from 23.22.194.120
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
How to get search keywords from a referring page
internetguy




msg:1255811
 2:16 pm on May 22, 2004 (gmt 0)

I would like to use PHP to extract the search keywords from a referring page. For example, if someone searches on Google for 'fancy widgets' and clicks through to my site, I would like to be able to extract those keywords and store them in a MySQL database.
It's like the way awstats shows the keywords being searched to get to my site.

For example,
[google.com...]

and I would like to extract the words 'toddler toys'.

I'd appreciate any help you can give.

Thanks!

[edited by: jatar_k at 4:58 pm (utc) on May 22, 2004]
[edit reason] generalized search terms [/edit]

 

ergophobe




msg:1255812
 3:45 pm on May 22, 2004 (gmt 0)

This should do it more or less, though there might be some special cases that don't work.

$param_array = explode('&', $_SERVER['HTTP_REFERER']);
foreach ($param_array as $param)
{
if ($param{0} == 'q') // if first char is q, it's your google query.
{
$word_string = substr($param, 2); // strip 'q='
}
}
$word_string = str_replace('"', '', urldecode($word_string));

$words = explode(' ', $word_string);

echo "<pre>\n";
print_r($words);
echo "</pre>\n";

internetguy




msg:1255813
 9:57 pm on May 23, 2004 (gmt 0)

Thanks for replying. I'm new to MySQL and PHP so I still can't get it to work. Don't worry about it for now.

goehner




msg:1255814
 4:34 pm on May 30, 2004 (gmt 0)

Hello everybody,
I was searching for something like this myself and have taken the liberty to alter ergophobe's code a little (I hope you don't mind ergophobe 8-)) in order for it to also get rid of "?" (at the start of the query string) in addition to "&".

I developed it for use on google.de but i think it should work on every version of Google as they all use "q=" to start the keyword query string - at least AFAIK.

----- Start PHP code -----
$query_part=strstr($_SERVER['HTTP_REFERER'], "q=");

$param_array=explode('&', $query_part);

foreach ($param_array as $param)
{
if ($param{0} == 'q') // if first char is q, it's your google query.
{
$word_string = substr($param, 2); // strip 'q='
}
}
$keywords = str_replace('"', '', urldecode($word_string));

print $keywords;
----- End PHP code -----

The script extracts the keywords used and assigns them to the variable $keywords.

I hope anybody finds this script useful - my thanks go to the original author ergophobe for providing his example.

Kind regards,
Georg

httpwebwitch




msg:1255815
 4:24 am on Jun 1, 2004 (gmt 0)

here's what you need; thoroughly tested & proven with over 10,000,000 referrals so far with no errors.

requires table searchterms
referertoURL [varchar 255]
keywords [varchar 255]
refererURL [varchar 255]
datetime [datetime]
userIP [varchar 100]

referer_sniffer.php

<?php
$qs=after("?",$HTTP_SERVER_VARS['HTTP_REFERER']);
$dn=before("?",$HTTP_SERVER_VARS['HTTP_REFERER']);
$pairs=explode("&",$qs);
if (eregi("google",$dn)){
foreach ($pairs as $k=>$v){
$args=explode("=",$v);
if ($args[0]=="q" $args[0]=="as_q" $args[0]=="as_epq"){
if (strlen($args)>0 && $args[1]!="0"){
$keys=urldecode($args[1]);
}
}
}
}elseif(eregi("msn",$dn)){
foreach ($pairs as $k=>$v){
$args=explode("=",$v);
if ($args[0]=="q"){
if (strlen($args[1])>0 && $args[1]!="0"){
$keys=urldecode($args[1]);
}
}
}
}elseif(eregi("yahoo",$dn)){
foreach ($pairs as $k=>$v){
$args=explode("=",$v);
if ($args[0]=="p"){
if (strlen($args[1])>0 && $args[1]!="0"){
$keys=urldecode($args[1]);
}
}
}
}elseif(eregi("aol",$dn)){
foreach ($pairs as $k=>$v){
$args=explode("=",$v);
if ($args[0]=="query"){
if (strlen($args[1])>0 && $args[1]!="0"){
$keys=urldecode($args[1]);
}
}
}
}elseif(eregi("hotbot",$dn)){
foreach ($pairs as $k=>$v){
$args=explode("=",$v);
if ($args[0]=="query"){
if (strlen($args[1])>0 && $args[1]!="0"){
$keys=urldecode($args[1]);
}
}
}
}

if (isSet($keys) && strlen($keys)>0){
mysql_select_db("mydbase");
$query="INSERT INTO searchterms (referertoURL,keywords,refererURL,datetime,userIP) VALUES ('http://".$HTTP_SERVER_VARS['SERVER_NAME'].$HTTP_SERVER_VARS['SCRIPT_NAME'] ."','".strtolower(trim($keys))."','".$HTTP_SERVER_VARS['HTTP_REFERER'] ."','" .date("Y-m-d h:i:s",mktime())."','".$HTTP_SERVER_VARS['REMOTE_ADDR']."')";
mysql_query($query);
}

function after ($this, $inthat){
if (!is_bool(strpos($inthat, $this)))
return substr($inthat, strpos($inthat,$this)+strlen($this));
}

function before ($this, $inthat){
return substr($inthat, 0, strpos($inthat, $this));
}

?>

[1][edited by: jatar_k at 5:55 am (utc) on June 1, 2004]
[edit reason] fixed sidescroll [/edit]

httpwebwitch




msg:1255816
 4:29 am on Jun 1, 2004 (gmt 0)

note: save the script (above) as a file, and include() it at the bottom of all your pages. Enjoy!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved