Intranet Hit counter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Patricius Luciani
    New Member
    • Jun 2006
    • 6

    Intranet Hit counter

    Hi all php experts,

    Need some help here. I need a hit counter for use in an Intranet.
    Unlike the Internet, where i can hook on to an external server.
    Intranet means I need the code itself for the counter to work.

    Can anyone recommend a basic code for such a counter.

    Well be very grateful,
    An amature programmer
  • sashi
    Recognized Expert Top Contributor
    • Jun 2006
    • 1749

    #2
    Hi there,

    hope the below sample will help you to get started.. good luck my fren..

    Code:
    Note: You will need to find some digits from somehere, name them as 0.gif, 1.gif, 2.gif etc. The 
    counter logs hits only, there is no support for cookies or host logging in this version.
    
    To get it to work, simpy modify the code bellow this paragraph where appropriate and upload 
    to your server. Then on php3 pages which you want the counter to appear, use 
    
    <? include("script location"); ?>
    
    <?
    
    ///////////////////////////////////////////////////////////////////////////////////
    //|
            $fileloc = "/home/mysite/hits.txt";
    //|
    //| Change this to the url directory containing the images, including trailing /
    //|
            $imagedir = "http://www.mysite.com/images/";
    //|
    //| If the counter is broken, or there is a problem, it will display nothing unless you set this to 1
    //|
            $debug = 0;
    //|_________________________________________________________________________
    ________
    ////////////////////////////////////////////////////////////////////////////////////
    
    
    if(is_file($fileloc))
    {
            if($hits = file($fileloc) AND $file = fopen($fileloc, "w"))
            {
                    $hits = trim($hits[0]) + 1;
                    fputs($file, $hits);
                    $length = strlen($hits);
                    fclose($file);
                    $file = fopen($fileloc, "r");
                    echo "<table cellspacing=0><tr>\n";
                    for($repeat = 1; $repeat <= $length; $repeat++)
                    {
                            $number = fgetc($file);
                            echo "<td><img src=\"$imagedir$number.gif\" alt=\"$number\">
    \n";
                    }
                    echo "</tr></table>\n";
                    fclose($file);
            }
            else
            {
                    if($debug != 0)
                    {
                            echo "Error: Could not open the file, or file does not contain a 
    valid number";
                    }
            }
    }
    else
    {
            if($debug != 0)
            {
                    echo "Error: Hit file does not exist, or there has been a disk failure";
            }
    }
    
    ?>

    Comment

    • Patricius Luciani
      New Member
      • Jun 2006
      • 6

      #3
      Many thanks for your help. Really appreciate it. However, i got another code that may work

      <?php
      session_start ();

      // get current hit
      $opFile = fopen ("counter.tx t", "r");
      $handle = fread ($opFile, filesize ("counter.txt") );
      fclose ($opFile);

      // if new session
      if (!isset ($_SESSION['hit'])){

      // set session
      $_SESSION['hit'] = TRUE;

      // add the hit
      $handle = $handle + 1;

      // print javascript
      echo 'document.write ("'.$handle. ' Hits");';

      // put new hit to db
      $opFile = fopen ("counter.tx t", "w");
      fwrite ($opFile, $handle);
      fclose ($opFile);

      // else
      }else{

      // print only
      echo 'document.write ("'.$handle. ' Hits");';
      }
      ?>

      then create counter.txt with number (let's say: 0) as its content... nothing more not even a single space (to make sure $handle + 1 won't produce error)

      then on your html page, put:

      <script language="JavaS cript" src="counter.ph p" type="text/JavaScript"></script>

      at the place where you want the counter to appear...

      Would this be a better option?

      Comment

      Working...