The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


Results 1 to 5 of 5

Thread: tracking webstatistics using php

  1. #1

    tracking webstatistics using php

    is it possible to write a simple script that tracks the traffic of specific html pages.

    right now the client has an all html site. But he wants to know which pages are being accessed. Can this be done in php or javascript?

  2. #2
    Most hosting monitors traffic automagically. But if you insist:

    <body onload="TrafficScript()">

    then make a javascript function that uses xmlhttprequest

    <script type="text/javascript">
    var xmlhttp;
    function TrafficScript()
    {
    xmlhttp=null;
    if (window.XMLHttpRequest)
    {// code for all new browsers
    xmlhttp=new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {// code for IE5 and IE6
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (xmlhttp!=null)
    {
    xmlhttp.onreadystatechange=state_Change;
    xmlhttp.open("GET","traffic.php",true);
    xmlhttp.send(null);
    }
    else
    {
    alert("Your browser does not support XMLHTTP.");
    }
    }
    </script>

    http://www.w3schools.com/xml/xml_http.asp

    I'm totally just making this up as I go along as I've never tried any of this.
    Then in the php file traffic.php just check if the $_SERVER['HTTP_REFERER'] is set and if so add it to the database if it isn't already added. If it is then ++ the counter or something. Again I totally made this all up.

    //edit store IPs and track unique hits
    Last edited by Sirisian; February 5th, 2008 at 04:44 PM.

  3. #3
    18
    posts
    chown -R us ./base
    You can always write a php solution instead to keep all load server-side

    you can grab a lot of information from the $_SERVER[] global in PHP which may well be of use

    I've written a number of custom tracking tools in php - about the only things you *need* to use javascript for are detecting flash versions and screen resolutions/colours - everything else can be found out using php

    good luck!

  4. #4
    I`m using something like this:

    <?

    $fname = "./l.log";
    if ($_GET['action']=="show"){
    // loading file (print it)
    // $fname = "./l.log";
    $fh = fopen($fname, 'r');
    $f1 = fread($fh, filesize($fname));
    header("Expires: Thu, 01 Jan 1970 00:00:01 GMT");
    echo '
    <html>
    <table width=800 border=1>
    <tr>
    <td width=200>date</td><td width=200>Server info</td><td width=200>IP</td><td width=200>Referer</td>
    </tr>
    ';
    // printint log results!
    $f1 = explode("<<<M>>>", $f1);
    $c = count($f1);
    //print "c=".$c;
    for($i=0;$i<$c-1;$i++){
    $t = explode('|-|-|', $f1[$i]);
    $time = $t[0];
    $sname=$t[1];
    // $scrname=$t[2];
    $ip = $t[2];
    $host = $t[3];
    $lang = $t[4];
    $ref = $t[5];
    $uagent=$t[6];
    //print "$f1[$i]\r\n";
    print "<tr><td>$time</td><td>$sname<br>$uagent</td><td>$ip<br>$host<br>$lang<br></td>";
    if ($ref <> ""){
    print "<td><a href=\"$ref\">$ref</a></td>";
    }else{
    print "<td>none</td>";
    };
    print "</tr>\r\n";
    }
    echo '
    </table>
    </html>
    ';
    print "<<<<<<<<<<".fread($fh, filesize($fname)).">>>>>>>>>>";
    exit;
    }
    if ($_GET['action']=="del"){
    // deleting a log file
    unlink($fname);
    print "done";
    exit;
    }
    //
    megalog("./");

    function megalog($path){
    $fname = "./l.log";
    $fh=fopen($fname, 'a+');
    $log = buildlogstr();
    fputs($fh, $log);
    fclose($fh);
    }
    function buildlogstr(){
    // build a log string
    $tmp = array(date("H:m:s d.m.Y O"), $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'], $_SERVER['REMOTE_ADDR'],$_SERVER['REMOTE_HOST'],$_SERVER['HTTP_ACCEPT_LANGUAGE'], $_SERVER['HTTP_REFERER'], $_SERVER['HTTP_USER_AGENT']);
    $log = implode("|-|-|", $tmp);
    $log .= "<<<M>>>";
    return $log;
    }
    ?>
    Last edited by webovich.alex; February 11th, 2008 at 01:57 AM.

  5. #5
    18
    posts
    chown -R us ./base
    that's a nice log-based solution - but I've always found storing this type of information in a database allows a lot more analysis to be done

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012