homepage Welcome to WebmasterWorld Guest from 54.197.110.151
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
looking for script that detects origin and keywords
michal317

5+ Year Member



 
Msg#: 13351 posted 5:39 am on Jun 29, 2006 (gmt 0)

Hi!
I'l looking for a script that can detect the origin of people that enter my site (MSN, google) and can tell as well with which keywords they enter.

thanks!

 

eelixduppy

WebmasterWorld Senior Member eelixduppy us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 13351 posted 5:44 am on Jun 29, 2006 (gmt 0)

$_SERVER["HTTP_REFERER"] will give you the the site the people are coming from, however this isn't entirely accurate. Here's information on Predefined Variables [us2.php.net]. Good luck!

avant_garde

5+ Year Member



 
Msg#: 13351 posted 5:50 am on Jun 29, 2006 (gmt 0)

Just look at the $_SERVER['HTTP_REFERER'] variable to see where the visitor came from. Then, any keywords will usually be in the query string of the referer in the form q=keywords. You can just use some string manipulation to extract them.

frozenpeas

5+ Year Member



 
Msg#: 13351 posted 5:25 pm on Jun 29, 2006 (gmt 0)

Found this within this forum and it was very helpful as a starting point - just pop it into a file and include it into the footer of everypage:

// PHP START
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));
}
$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){
$query="INSERT INTO searchterms (search_referertoURL,search_keywords,search_refererURL,search_datetime,search_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);
}
// PHP END

// CREATE A TABLE
CREATE TABLE searchterms (
search_id int(11) unsigned NOT NULL auto_increment,
search_referertoURL varchar(255) NOT NULL default '',
search_keywords varchar(255) NOT NULL default '',
search_refererURL varchar(255) NOT NULL default '',
search_datetime datetime NOT NULL default '0000-00-00 00:00:00',
search_userIP varchar(100) NOT NULL default '',
PRIMARY KEY (search_id)
) TYPE=MyISAM;

// CREATE A TABLE

Example Query:

Pull top 10 search_keywords

$distinct = mysql_query("SELECT DISTINCT search_keywords, COUNT(*) as count from ".$prefix."_searchterms GROUP BY search_keywords ORDER BY count DESC, search_keywords LIMIT 10");
while($distinctresult = mysql_fetch_array($distinct)){
echo $distinctresult['count'].'no '.$distinctresult['search_keywords'].'<br>';
}

tigerflag

5+ Year Member



 
Msg#: 13351 posted 3:39 pm on Jul 10, 2006 (gmt 0)

Frozenpeas kindly wrote:

"Found this within this forum and it was very helpful as a starting point - just pop it into a file and include it into the footer of everypage:"

Could you please elaborate a little for the technically ignorant? (Me!)

Where would the file go, and how exactly do you include it in the footer of a page?

Does the file need to contain anything else?

Thanks!

Siri Amrit

Komodo_Tale

5+ Year Member



 
Msg#: 13351 posted 12:45 am on Jul 11, 2006 (gmt 0)

Have a look at this.

[xav.com...]

frozenpeas

5+ Year Member



 
Msg#: 13351 posted 4:22 pm on Jul 14, 2006 (gmt 0)

Sorry I assumed you knew about includes TigerFlag:

1. Create a database table as shown previously.

2. Create a file called stats.php and store it within a directory called "includes"
Inside this file paste this code and nothing else:
<?php
if (isset($HTTP_SERVER_VARS['HTTP_REFERER'])){
///////////////////////////////

CONNECT TO YOUR DATABASE HERE

///////////////////////////////
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));
}
$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){
$query="INSERT INTO searchterms (search_referertoURL,search_keywords,search_refererURL,search_datetime,search_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 ISSET
?>

3. Include this file within any php page you want to record the stats from using the following:
<?php require("includes/stats.php");?>

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