I have it in a file called 'analytics.php' and am just using include to pull it into the site.
However when I check my database for the stats it collects it says the page that is being visited is 'analytics.php' and not the page that is calling the file.
What am I doing wrong?
I have been hacking around with this code a bit, am sure it is not the best solution but I am still learning!
<?php
$ipaddress = $_SERVER['REMOTE_ADDR'];
$useragent = $_SERVER['HTTP_USER_AGENT'];
$pagevisited = $_SERVER['REQUEST_URI'];
$refurl = $_SERVER['HTTP_REFERER'];
if($refurl == '')
{
$reftype = "Direct";
}
else
{
// Check if user came from external page
if (preg_match ("/mydomain/i", "$refurl"))
{
//Not an external referal
$reftype = "None";
}
else
{
//Check if it is search engine
searchEngine();
if($searchengine == "yes")
{
$reftype = "Organic";
getKeywords();
}
else
{
//Normal Referral
$reftype = "Referral";
}
}
}
function searchEngine()
{
$refer = parse_url($_SERVER['HTTP_REFERER']);
$host = $refer['host'];
if(strstr($host,'google'))
{
$searchengine = "yes";
$sedomain = "Google";
return $sedomain;
}
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;
}
}
$con = mysql_connect(***, ***, ***) or die ("Unable to connect to MySQL server.");
mysql_query ("INSERT INTO `analytics` (`access_time`, `referer`, `ref_type`, `ipaddress`, `browser_info`, `se_query`, `se_domain`, `accessed_page`) VALUES (CURRENT_TIMESTAMP, '$refurl', '$reftype', '$ipaddress', '$useragent', '$keywords', '$sedomain', '$pagevisited')");
mysql_close($con)
?>


Sign In
Create Account


Back to top











