By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 804 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

Intranet Hit counter

P: 6
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
Jul 7 '06 #1
Share this Question
Share on Google+
2 Replies


sashi
Expert 100+
P: 1,754
Hi there,

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

Expand|Select|Wrap|Line Numbers
  1. Note: You will need to find some digits from somehere, name them as 0.gif, 1.gif, 2.gif etc. The 
  2. counter logs hits only, there is no support for cookies or host logging in this version.
  3.  
  4. To get it to work, simpy modify the code bellow this paragraph where appropriate and upload 
  5. to your server. Then on php3 pages which you want the counter to appear, use 
  6.  
  7. <? include("script location"); ?>
  8.  
  9. <?
  10.  
  11. ///////////////////////////////////////////////////////////////////////////////////
  12. //|
  13.         $fileloc = "/home/mysite/hits.txt";
  14. //|
  15. //| Change this to the url directory containing the images, including trailing /
  16. //|
  17.         $imagedir = "http://www.mysite.com/images/";
  18. //|
  19. //| If the counter is broken, or there is a problem, it will display nothing unless you set this to 1
  20. //|
  21.         $debug = 0;
  22. //|_________________________________________________________________________
  23. ________
  24. ////////////////////////////////////////////////////////////////////////////////////
  25.  
  26.  
  27. if(is_file($fileloc))
  28. {
  29.         if($hits = file($fileloc) AND $file = fopen($fileloc, "w"))
  30.         {
  31.                 $hits = trim($hits[0]) + 1;
  32.                 fputs($file, $hits);
  33.                 $length = strlen($hits);
  34.                 fclose($file);
  35.                 $file = fopen($fileloc, "r");
  36.                 echo "<table cellspacing=0><tr>\n";
  37.                 for($repeat = 1; $repeat <= $length; $repeat++)
  38.                 {
  39.                         $number = fgetc($file);
  40.                         echo "<td><img src=\"$imagedir$number.gif\" alt=\"$number\">
  41. \n";
  42.                 }
  43.                 echo "</tr></table>\n";
  44.                 fclose($file);
  45.         }
  46.         else
  47.         {
  48.                 if($debug != 0)
  49.                 {
  50.                         echo "Error: Could not open the file, or file does not contain a 
  51. valid number";
  52.                 }
  53.         }
  54. }
  55. else
  56. {
  57.         if($debug != 0)
  58.         {
  59.                 echo "Error: Hit file does not exist, or there has been a disk failure";
  60.         }
  61. }
  62.  
  63. ?>
  64.  
Jul 7 '06 #2

P: 6
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.txt", "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.txt", "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="JavaScript" src="counter.php" type="text/JavaScript"></script>

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

Would this be a better option?
Jul 14 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.