TinyPortal
Development => Block Codes => Topic started by: Ianedres on September 05, 2007, 05:07:23 PM
This is a very simple daily hit counter. It will increment the count on each view until the next day (determined by your server's date) which will reset back to 1.
This is slightly different from my first version posted on TP Lite during the server changeover.
The script defaults to displaying the counter, but if you place 'silent' in the URL (with a ? between the scriptname and 'silent') if will suppress the output yet still increment the counter.
There is no error checking for file access; errors are suppressed also.
If you place in a block, remove the '<?php' from the first line and '?>' from the last line.
<?php
// Daily Counter Script
// September 4, 2007
// Tim Antley - www.bayoumx.com
// For Use With SMF & TinyPortal
// Stores the day of week and hit count in a file.
// If current day is same as file's day-stamp, counter is incremented by one.
// If day is different, counter resets to 1.
// Specify 'silent' in URL will suppress output after incrementing count.
// Place a question mark between script name; ie. daily_counter.php?silent
// There is no error checking for the file.
$counter_file = 'daily_counter.csv'; // can use your own filename here.
// Should not have to edit anything below this line.
$today = getdate(); // Array for today's information
$handle = @fopen($counter_file, "r"); // Read-only access
$data = @fgetcsv($handle, 1000, ','); // Reads file comma-separated values & places into array $data.
@fclose($handle);
$counter = $data[1];
if($data[0]==$today['mday']) // Check file's day-stamp against current day.
{
$counter++;
}
else
{
$counter=1;
}
$output = $today['mday'].', '.$counter; // Format output into comma-separated values for file.
$handle = @fopen($counter_file, "w");
@fwrite($handle, $output);
@fclose($handle);
if(!isset($_GET['silent'])) // Display output unless 'silent' appears in URL.
{
echo $counter;
}
?>
cool. works. would be cooler if it was unique hits vs counting everytime i hit refresh or went into another page.
I thought about that also- however, with my PHP coding at a very elementary level, it was the best I could do for now. 8)