Forum Moderators: coopster
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]
$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";
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
requires table searchterms
referertoURL [varchar 255]
keywords [varchar 255]
refererURL [varchar 255]
datetime [datetime]
userIP [varchar 100]
referer_sniffer.php
<?php if (isSet($keys) && strlen($keys)>0){ function after ($this, $inthat){ function before ($this, $inthat){ ?> [1][edited by: jatar_k at 5:55 am (utc) on June 1, 2004]
$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]);
}
}
}
}
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);
}
if (!is_bool(strpos($inthat, $this)))
return substr($inthat, strpos($inthat,$this)+strlen($this));
}
return substr($inthat, 0, strpos($inthat, $this));
}
[edit reason] fixed sidescroll [/edit]