Jump to content

PHP Include Problem

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
adzeds

adzeds

    Newbie

  • Members
  • PipPip
  • 27 posts
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?

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)

?>


#2
Jacki

Jacki

    Learning Programmer

  • Members
  • PipPipPip
  • 80 posts
Try:
$pagevisited = $_SERVER["PHP_SELF"]; 
Bye!
Posted Image

Posted Image

#3
adzeds

adzeds

    Newbie

  • Members
  • PipPip
  • 27 posts
Gives me the same result!

#4
adzeds

adzeds

    Newbie

  • Members
  • PipPip
  • 27 posts
How would you include this code in the page? Maybe that is the problem?

#5
Jacki

Jacki

    Learning Programmer

  • Members
  • PipPipPip
  • 80 posts
I tryed using:
include 'page.php';
and it's worked... how do you include the php file?
Posted Image

Posted Image

#6
adzeds

adzeds

    Newbie

  • Members
  • PipPip
  • 27 posts
Would it cause a problem if I am using:
include 'http://www.domain.com/analytics.php'; 


#7
Jacki

Jacki

    Learning Programmer

  • Members
  • PipPipPip
  • 80 posts
I don't think, but you can try simple:
include 'analytics.php';  
You can also see if there're any var like these here $_SERVER vars.
Posted Image

Posted Image

#8
Guest_Jaan_*

Guest_Jaan_*
  • Guests
well.. If you try this include with full url.. maybe it's disabled in your php_ini.. so you should check it..