-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcounter.php
More file actions
executable file
·34 lines (27 loc) · 929 Bytes
/
counter.php
File metadata and controls
executable file
·34 lines (27 loc) · 929 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
define("COUNTER_START_VALUE", 0);
define("COUNTER_LOG", "idious-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);
return $count;
}
?>