1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Include Problem!!!

Discussion in 'PHP' started by adzeds, Oct 27, 2009.

  1. #1
    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?
     
    adzeds, Oct 27, 2009 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Posting your code might help ;)
     
    JAY6390, Oct 27, 2009 IP
  3. AsHinE

    AsHinE Well-Known Member

    Messages:
    240
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    138
    #3
    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.
     
    AsHinE, Oct 27, 2009 IP
  4. adzeds

    adzeds Well-Known Member

    Messages:
    1,209
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    100
    #4
    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:
     
    adzeds, Oct 27, 2009 IP
  5. adzeds

    adzeds Well-Known Member

    Messages:
    1,209
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    100
    #5
    Anyone got any help/advice?
     
    adzeds, Oct 27, 2009 IP
  6. xenon2010

    xenon2010 Peon

    Messages:
    237
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    can you explain more in details?
     
    xenon2010, Oct 27, 2009 IP
  7. adzeds

    adzeds Well-Known Member

    Messages:
    1,209
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    100
    #7
    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!
     
    adzeds, Oct 27, 2009 IP