Web Analytics Made Easy -
StatCounter php code to track page visits - CodingForum

Announcement

Collapse
No announcement yet.

php code to track page visits

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • php code to track page visits

    I would like to share a php code that I developed that I think is very usefull. It tracks which of my web pages are visited, the IP address and date. It then writes the info to a txt file called track.txt. Below is a php file called track.php

    PHP Code:
    <?php

    $ipaddress 
    $_SERVER['REMOTE_ADDR'];
    $date date ("M dS H:i:s");

    $message "$page _ $ipaddress _ $date\n";


    $File "track.txt";
    $Open fopen($File"a+");
    if (
    $Open){

    fwrite($Open"$message");
    fclose ($Open);
    }

    ?>
    And the code below goes into each of my web pages. In this case the "Columbia Brewing Company" page. I only change the title on each page


    PHP Code:
    <?php
         $page 
    ='Columbia Brewing Company';

    include 
    "track.php";
    include 
    "header.php";
    include 
    "nav.php";
        echo 
    "<h1>$page</h1>";
    ?>

    I get the output below on a txt file, which tells me the pages that where visited along with the IP address and date. stubby.ca is always the first page



    stubby.ca _ 65.50.18.119 _ Feb 16th 03:04:46
    Labatt Brewing Company _ 65.50.18.119 _ Feb 16th 03:04:49
    Carling Breweries Ltd. _ 65.50.18.119 _ Feb 16th 03:04:51
    O'Keefe Brewing Company _ 65.50.18.119 _ Feb 16th 03:04:54
    O'Keefe Brewing Company _ 65.50.18.119 _ Feb 16th 03:04:56
    stubby.ca _ 65.50.18.119 _ Feb 16th 03:05:13
    O'Keefe Brewing Company _ 65.50.18.119 _ Feb 16th 03:05:17
    stubby.ca _ 24.77.175.78 _ Feb 16th 03:42:49
    stubby.ca _ 24.101.53.42 _ Feb 16th 05:21:03
    stubby.ca _ 12.74.202.78 _ Feb 16th 05:36:47


    Leonard Whistler
    http://www.stubby.ca
    Last edited by Len Whistler; Feb 18, 2004, 08:10 PM.
    Leonard Whistler

  • #2
    any reason why you save the page-titleand not, for instance the filename or adress (which you could do dynamically with
    $_SERVER['SCRIPT_FILENAME']
    or
    $_SERVER['PHP_SELF']
    so that you don't need to 'freeze' your pagetitle)

    I suppose that the fileadress will be more stable and relevant then the printed title.

    Also, what are you planning to do with the logfile? If you plan on importing it into a db, then i would personnaly choose another seperator (like | ) and i would save the datetimevalues in a more universal format (or even the unix-timestamp)
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

    Comment


    • #3
      Thanks for the reply raf...


      The file name, instead of page title, sounds like a good idea. Later on when I get better at php/mysql I might add it to a DB.

      So far it tells me that most people who visit my site do not visit the other pages.



      Leonard Whistler
      http://www.stubby.ca
      Leonard Whistler

      Comment


      • #4
        Beautiful, just what I needed: a simple code that records the IP address of each visitor of each page and the time of the visit, and stores them permanently in a file. You would be surprised how many 'professional' web analytics tools either don't provide the ability to view visitors' IP addresses, or else else limit the record to the last X number of visitors. Thank you so much for this very useful code.

        Comment

        Working...
        X