Hi
A simple method using a single text file with predefined column widths. Using one file, one IO handle! It's 100% dynamic!
<?
/*
* the counter name (script) you are tracking
* can be passed by $var or http query
*/
$script = $_REQUEST['id'];
/*
* the counters total hits max field length
*/
$digits_max = 10; // the max counter digits
/*
* the counter id max field length
* it should set to the maximum script name
* in total characters IE: (file.zip = 8)
*/
$script_max = 40;
/*
* windows chr ( 13 ) . chr ( 10 ) or "\r\n"
* unix just use chr ( 10 ) or "\n",
* mac use chr ( 13 ) or "\r"
*/
$sys_eol = chr ( 13 ) . chr ( 10 );
/*
* the location of the counter file (including path)
*/
$counter = 'counter.txt';
// usage
my_counter ( $digits_max, $script_max, $sys_eol, $counter, $script );
function my_counter ( $max, $len, $eol, $file, $page = 'none_sent' )
{
if ( ! file_exists ( $file ) )
{
$fp = fopen ( $file, 'w' );
fputs ( $fp, sprintf ( '%' . $max . 's', 1 ) . '|' . sprintf ( '%' . $len . 's', $page ) . $eol );
}
else
{
$cc = 0;
$ll = strlen ( $eol );
$fd = $max + 1;
$sf = $len + $ll;
$id = 0;
$fp = fopen ( $file, 'r+' );
while ( ! feof ( $fp ) )
{
$next = ( $fd + $cc );
fseek ( $fp, $next, SEEK_SET );
$data = trim ( fgets ( $fp, $sf ) );
if ( $data == $page )
{
fseek ( $fp, $cc, SEEK_SET );
$data = trim ( fgets ( $fp, $fd ) );
fseek ( $fp, $cc, SEEK_SET );
fputs ( $fp, sprintf ( '%' . $max . 's', ++$data ) );
$id = 1;
break;
}
else
{
$cc += $fd + $sf;
}
}
if ( ! $id )
{
fseek ( $fp, 0, SEEK_END );
fputs ( $fp, sprintf ( '%' . $max . 's', 1 ) . '|' . sprintf ( '%' . $len . 's', $page ) . $eol );
}
}
fclose ( $fp );
}
?>
abc