#!/usr/bin/perl --
# hit_counter.pl
# read the users.sav file and display the contents.

use CGI;
$query = new CGI;
$default_file = 'nick_hits.sav'; # The file name.
$hits_file = 'nick_hits.dat'; # Save the dates of past hits.

print $query->header;
print $query->start_html("Hit Counter");

# Read the stuff from the saved file.
$query = &restore_parameters($query);

print "<BODY>";
print "<P>";
$value = $query->param('hits') + 1;

print "<CENTER>";
print "You are visitor<BR>";
print "<TABLE bgcolor='#000000' border='0' cellpadding=0 cellspacing=0 leftmargin=0 topmargin=0 rightmargin=0 bottommargin=0>";
print "<TR><TD>";
print "<IMG SRC=hgrade.jpg width=32 height=32>";
print "</TD><TD>";
print "<a href='nick_stats.pl' target='frame2'>";
print "<font color='#ffffff' size='4'>";
#print "<P STYLE='FONT-SIZE:16pt; COLOR:FFFFFF'>$value</P></a>";
print "$value</a>";
print "</TD><TD>";
print "<IMG SRC=hgrade2.jpg width=32 height=32>";
print "</TD></TR></TABLE>";
print "</CENTER>";
print "</BODY>";

$query->param('hits',$value);
&save_parameters($query);
&save_past_hits();

# Here we print out a bit at the end
print $query->end_html;

# The subroutine which reads from the file.
sub restore_parameters
{
    local($query) = @_;
    local($filename) = $default_file;
    if (open(FILE,$filename))
    {
	  $query = new CGI(FILE);
	  close FILE;
    }
	else
	{
	  # fill with a default value initially.
        print "$default_file does not exist.";
    }
    return $query;
}

#
sub save_parameters
{
    local($query) = @_;
    local($filename) = $default_file;
    if (open(FILE,">$filename"))
	{
	  $query->save(FILE);
	  close FILE;
    }
	else
	{
	  print "<STRONG>Error:</STRONG> couldn't write to file $filename: $!\n";
    }
}

#
sub save_past_hits
{
	local($filename) = $hits_file;

	$the_date = localtime(time());

	if (open(FILE,">>$filename"))
	{
		print FILE $the_date."\n";
		close(FILE);
	}
}
# EOF
