I am trying to write a small script to collect some web stats. 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?
What do you insert in database? I suppose it is $_SERVER['PHP_SELF'] which is wrong, try $_SERVER['REQUEST_URI']. Or it would be better if you show us your analytics.php Don't forget [Ð ÐÐ ][/Ð ÐÐ ] tags in that case.
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 ("/www.davidshawblog.com/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) ?> PHP:
OK. I have fixed my initial problem! I was including the file as include 'http://www.davidshawblog.com/analytics.php' when I should have just been using 'analytics.php' My problem now is that that I cannot seem to get my searchEngine or getKeywords functions to work!