Counters

Use of counters requires a writeable file. To create the file, in a shell window:
shell> cd [The directory of your PHP file.]
shell> touch counter.txt
shell> chmod 666 counter.txt
These are the NetCloak Counter commands.

<INSERT_COUNT counter>

Increment and print the counter.

Example

This page has been accessed
<?php InsertCount("counter.txt"); ?> times.
The file must already be created with rw access to the user nobody.

InsertCount( file )

<?php
    //-----------------------------------------------------
    // InsertCount($file,$increment)
    // - - - - - - - - - - - - - - -
    // Increments the count by 1 in the file $counter
    //                      unless $increment is false.
    // Prints the counter value.
    //
    // PHP Example:
    //       
    //       echo 'The page has been accessed ';
    //       InsertCount("counter.txt");
    //       echo ' times.';
    //
    // See: GetCount()
    //-----------------------------------------------------
    function InsertCount( $file, $increment = true )
    {
    	print GetCount( $file, $increment );
    }
 ?>

<INSERT_COUNT# counter>

Print but do not increment the counter.

Example

The counter reads <?php InsertCount("counter.txt",false); ?>.
The file must already be created with rw access to the user nobody.

See: InsertCount()


<INSERT_COUNT* counter>

Increment but do not print the counter.

Example

<?php Count("counter.txt"); ?>
The counter has been incremented.
The file must already be created with rw access to the user nobody.

GetCount( counter )

<?php
    //-----------------------------------------------------
    // GetCount($file,$increment)
    // - - - - - - - - - - - -
    // Increments the count by 1 in the file $counter
    //                      unless $increment is false.
    // Returns the counter value.
    //
    // PHP Example:
    //       
    //       $count = Count("counter.txt");
    //       if( $count < 50 )
    //           print "You are a visiting a new page.";
    //       else
    //           print "You are visitor #$count.";
    //-----------------------------------------------------
    function GetCount( $file, $increment = true )
    {
        if( !($fp = fopen( $file, "r" )) )
        {
            print "\nError reading counter file: $file.\n";
            $count = 0;
        }
        else
        {
            $count = strtok(fgets($fp,80), " ");
            $time = strtok(" ");
            fclose($fp);
        }
        
        if( !$increment )
            return $count;
        
        $count++;
        
        if( !$time )
            $time = time();
        
        if( !($fp = fopen( $file, "w" )) )
        {
            print "\nError writing counter file: $file.\n";
        }
        else
        {
            fwrite($fp, sprintf( "%d %d", $count, $time ));
            fclose($fp);
        }
        return $count;
    }
 ?>

<SHOW_COUNT lowvalue highvalue counter>

Show the text if lowvalue <= counter value <= highvalue.

Example

<?php ShowCount(0,50,"counter.txt"); ?>
    
    Only the first 50 visitors get to see this.
    
<?php } ?>
The file must already be created with rw access to the user nobody.

ShowCount( lowvalue, highvalue, file )

<?php
    //-----------------------------------------------------
    // ShowCount($lowvalue,$highvalue,$file)
    // - - - - - - - - - - - - - - - - - - - -
    // Returns true if the counter value is between
    //   $lowvalue and $highvalue.
    //
    // See: GetCount()
    //-----------------------------------------------------
    function ShowCount($lowvalue,$highvalue,$file)
    {
    	$count = GetCount($file,false);
    	return ($lowvalue <= $count && $count <= $highvalue);
    }
 ?>

<HIDE_COUNT lowvalue highvalue counter>

Do not show the text if lowvalue <= counter value <= highvalue.

Example

<?php if( HideCount(0,200,"counter.txt") ) { ?>
    
    The first 200 visitors do not see this.
    
<?php } ?>
The file must already be created with rw access to the user nobody.

HideCount( lowvalue, highvalue, file )

<?php
    //-----------------------------------------------------
    // HideCount($lowvalue,$highvalue,$file)
    // - - - - - - - - - - - - - - - - - - - -
    // Returns false if the counter value is between
    //   $lowvalue and $highvalue.
    //
    // See: Count()
    //-----------------------------------------------------
    function HideCount($lowvalue,$highvalue,$file)
    {
    	$count = Count($file,false);
    	return ($count > $highvalue || $lowvalue > $count);
    }
 ?>

<INSERT_COUNTERDATE counter offset format>

Display the date the counter was created.

Example

The counter was created on
<?php InsertCounterDate("counter.txt"); ?>.
The file must already be created with rw access to the user nobody.

InsertCounterDate( file )

<?php
    //-----------------------------------------------------
    // InsertCounterDate($file,$offset,$format)
    // - - - - - - - - - - - - - - - - - - - -
    // Prints the date the counter was created.
    //
    // See: GetCounterTime()
    //-----------------------------------------------------
    function InsertCounterDate($file, $offset=0, $format="long")
    {
    	$kHour = 60*60;
    	$time  = GetCounterTime($file);
    	
    	if( strcasecmp($format,"long") )
            print date("l, F j, Y",$time+$offset*$kHour);
        else
            print date("m/d/y"    ,$time+$offset*$kHour);
    }
 ?>

GetCounterTime( file )

<?php
    //-----------------------------------------------------
    // GetCounterTime($file)
    // - - - - - - - - - - - - - - - - - - - -
    // Returns the UNIX timestamp for when the counter file
    //   was created.
    //
    // On error, returns "" or 0.
    //-----------------------------------------------------
    function GetCounterTime($file)
    {
        if( !($fp = fopen( $file, "r" )) )
        {
            print "\nError reading counter file: $file.\n";
            $count = 0;
        }
        else
        {
            $count = strtok(fgets($fp,80), " ");
            $time = strtok(" ");
            fclose($fp);
        }
        return $time;
    }
 ?>

<INSERT_COUNTERTIME counter offset format>

Display the time of day the counter was created.

Example

The counter was created at
<?php InsertCounterTime("counter.txt"); ?>.
The file must already be created with rw access to the user nobody.

InsertCounterTime( file )

<?php
    //-----------------------------------------------------
    // InsertCounterTime($file,$offset,$format)
    // - - - - - - - - - - - - - - - - - - - -
    // Prints the time of day the counter was created.
    //
    // See: GetCounterTime()
    //-----------------------------------------------------
    function InsertCounterTime($file, $offset=0, $format="long")
    {
    	$kHour = 60*60;
    	$time  = GetCounterTime($file);
    	
    	if( strcasecmp($format,"long") )
    		print date("g:i:s A",$time+$offset*$kHour);
    	else
    		print date("g:i A"  ,$time+$offset*$kHour);
    }
 ?>