Click to See Complete Forum and Search --> : Page Hit Counter
Dark Dragon
06-04-2003, 09:46 AM
I managed to copy some CGI script for a page hit counter:
#!/user/bin/perl
local ($count);
$file = "count.dat";
open(COUNT, "$file");
$count =<COUNT>;
close(COUNT);
$count++;
open(COUNT, ">$file)";
print COUNT $count;
close (COUNT);
print "Content-Type: text/html\n\n";
print "<html><head>\n";
print "<title>Count Function</title>\n";
print "</head>\n";
print "<body>\n";
print "<count-$count\n";
print "</body></html>\n";
But I want to incorporate it into a graphic of its own so how do I do it?..I don't know where to ask but this seems the place to ask..
cmelnick
06-04-2003, 10:31 AM
The simplest way to do this would be to create 10 image files, 0-9, and then find each digit in the $count variable and use that top get the image name. There are ways to generate images on the fly, but they are a pain. This is much easier:
# This goes after the close(COUNT) line
print "Counter: ";
foreach $digit (split //, $count) {
print "<img src=\"pics/$digit.jpg\" border=0>";
}
print "<br>\n";
jeffmott
06-04-2003, 10:43 AM
local ($count);
$file = "count.dat";
open(COUNT, "$file");
$count =<COUNT>;
close(COUNT);
$count++;
open(COUNT, ">$file)";
print COUNT $count;
close (COUNT);This probably should be changed first. Since multiple people could be accessing your site at any given time, there could be multiple instances of this script running. If so, it's possible that multiple instances will reading or writing to the file at the same, resulting in a loss of hits or even corrupted data. So what you need to do is lock the file while you are working with it so all other processes have to wait until the current one is done with the file. e.g.,use Fcntl qw{:flock};
open COUNT, '+<count.dat' or die $!; # +< means to open the file with both read
# and write access
flock COUNT, LOCK_EX or die $!;
my $count = <COUNT>;
seek COUNT, 0, SEEK_SET or die $!; # closing and reopening the file will
truncate COUNT, 0 or die $!; # implicitly release the lock, so we have to
# manually set the file position back to the
# beginning and erase the previous contents.
print COUNT ++$count or die $!;
close COUNT or die $!;As for the graphics, there's nothing built into Perl, but there are always library modules available. In this case you probably want GD.pm - Interface to Gd Graphics Library (http://search.cpan.org/author/LDS/GD-2.07/GD.pm). You'll have to read the documentation there.
Dark Dragon
06-04-2003, 11:02 AM
Huh...methinks my head is going to implode. Too confusing...I think I am better off getting a counter from one of those sites offering free counters.
Thanks everyone!;)
Charles
06-04-2003, 11:08 AM
I warned you to learn Perl before you started playing aroung with it. Lets hope that none of the damage to your head was permanent.
Dark Dragon
06-04-2003, 11:17 AM
Damn! Why does it have to be so bleedin' complicated anyways?? I am not really geared for programming languages...find it too tedious...but thanks for the help.
Jeff, that image creation stuff sounds really interesting.. I'm gonna have to learn how to do that one day.. When I get a CGI host. :(
But for now it's PHP..
Jona