• 0

Hit Counters


Question

14 answers to this question

Recommended Posts

  • 0

My quick CheesyHitCounter was written in 10 minutes in order to quickly gather the average amount of hits.

Here's the source code.

<?php
/* PHP CheesyHitCounter v1
   Copyright (c) Alexandre Gauthier
   for Paris Glove of Canada Ltd

   NOTE: Some of the variables have been stripped
   because the local PHP system would not like them.
   I have no clue why.

   Stay away from my code, filthy demonic jscript!
/**************************************************/

// Declare basic database variables
$database_host = "yourhost"; // Host
$database_user = "yourusername"; // Username
$database_password = "yourpassword"; // Password
$database = "yourdbname"; // Database Name
$table_name = "yourtablename";  // Table Name

// Variables used to connect to Database Backend
$connect = mysql_connect($database_host,$database_user,$database_password) or die("Unable to connect to Database Backend -- Please advise admin@parisglove.com.");
$db = mysql_select_db($database,$connect) or die("Unable to select database because an error occured."); //I dunno. I'm lazy for errs anyways.

/* Add a hit to our counter */

function hitcountAdd() {
          // Get current counter value from database
          $query_getCounterValue = "SELECT num_hits_value FROM c_hit_counter";
          $query_result = mysql_query($query_getCounterValue);
          while ($counter_array = mysql_fetch_array($query_result)){
                 $counterValue = $counter_array[num_hits_value];
          }
          // Add 1 to counterValue
          $counterValue++;
          // echo $counterValue; // For debugging purposes, show me what you got.
          // Insert new value into Database Backend
          $query_updateCounterValue = "UPDATE c_hit_counter SET num_hits_value='$counterValue'";
          mysql_query($query_updateCounterValue);
// Done. Lather, rinse, repeat.
}
// Display the amount of hits
function displayHits() {
         //Retrieve the value from the database
         $query_getCounterValue = "SELECT num_hits_value FROM c_hit_counter";
         $counter_tmp = mysql_query($query_getCounterValue) or die("Unable to retrieve counter value from Database Backend.");
         while($counter_array=mysql_fetch_array($counter_tmp)) {
               $counterValue=$counter_array[num_hits_value];
         }
         echo ($counterValue); // Display it
         return($counterValue); // In case you'd like to do something else with it.
}

// You should call this all-in-one function.
function hitCounter() {
// Adds a hit and then return displayHits
         hitcountAdd();
         $programOutput = displayHits();
         return($programOutput);
}
?>

Link to comment
Share on other sites

  • 0

1) Put this where u want the counter

<php include("counter.php") ?>

2)Make a file "counter.php" (make it in notepad is fine)

put this in it

<?php 
$filename= "hits.txt"; 
$fd = fopen ($filename , "r") or die (" cant open $filename have u chmoded $filename to 777"); 
$fstring = fread ($fd , filesize ($filename)); 
echo "$fstring"; 
fclose($fd); 

$fd = fopen ($filename , "w") or die ("cant open $filename have u chmoded $filename to 777"); 
$fcounted = $fstring + 1; 
$fout= fwrite ($fd , $fcounted ); 
fclose($fd); 
?>

3) create a file "hits.txt"

CHMOD it to 777 (allows the script to write to it)

Link to comment
Share on other sites

  • 0

ok aaronsam, so i've put the bit of code in my website (just on some line), then i've created counter.php. Then i uploaded them to the same dir on the net. I also made hits.txt, but what is chmoded? and how do i do it? And then do i upload that to the same place?

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.