Create visitor_counter.php

This commit is contained in:
The Arrayser 2022-04-15 21:20:21 +02:00 committed by GitHub
parent 836fdbb374
commit e2061d5d02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

33
visitor_counter.php Normal file
View File

@ -0,0 +1,33 @@
<?php
define("COUNTER_START_VALUE", 0);
define("COUNTER_LOG", "visit_counter.log"); //name of file you want to use to save the counter value
/*************************************************************************************************/
function IncrementCounter()
{
$create_file = !file_exists(COUNTER_LOG);
if( !($fh = fopen(COUNTER_LOG, $create_file ? "x+b" : "r+b")) )
return "Error";
//do an flock here, maybe, I don't know :-)
//Reading current value of counter:
if($create_file)
$count = COUNTER_START_VALUE;
else
{
$count = (int)fread($fh, 9); //reads 9 digits (supposing max 1 billion count)
rewind($fh);
}
//Writing new counter value:
if(!fwrite($fh, ++$count))
return "Error";
if(!fclose($fh))
return "Error";
return str_pad($count, 9, '0', STR_PAD_LEFT);
}
?>