>> KYSCORP.COM TUTORIALS >> Perl Tutorials
Graphic counter
This counter script gives you the opportunity to count the number of visitors to your site, displaying the number in graphic format, here too you must have a full access to your CGI directory and the SSI (stands for Server Side Includes) must be supported by your server, the base code is:
## Define Vars ##
$basedir = "home/user/kyscorp/perl/";
$baseurl = "www.kyscorp.com/perl";
$multi_submit = "yes";
### Done with vars ###
### Do not edit anything beyond this line ###
## Get current tally ###
open(datafile,"$basedir/data\.txt") || die$!;
$data = <datafile>;
close(datafile);
### Done ###
### Add to count and print if not a multi-user ###
if($ENV{'HTTP_COOKIE'} && $multi_submit =~ /no/) {
&print_count;
}
else {
if($multi_submit =~ /no/) {
print "Set-Cookie: cookie-name = Unique \; ";
print "domain = ."; print "$baseurl \; ";
print "path = /cgi-bin \; ";
print "expires = Thursday, 11-Dec-2003 00:00:00 GMT \; ";
print "\n";
}
&add_num;
&print_count;
}
### Done with multi-check ###
### Subs ###
### Add num sub ###
sub add_num {
$data++;
open(dfile,">$basedir/data\.txt") || die$!;
flock("$basedir/data\.txt",2);
print dfile "$data";
flock("$basedir/data\.txt",8);
close(dfile);
}
### Done ###
### Print output sub ###
sub print_count {
print "content-type: text/html\n\n";
@nums = split(//,$data);
foreach $num (@nums) {
if($num =~ /\d/) {
print "<img src=http://$baseurl/counter/$num.gif>";
}
}
}
### Done ###
Modifications needed:
- Modify the first line of the script [#!/bin/perl] to suit the
configuration of your server (ask your host about it).
- Modify the 3rd line of the script changing the following [home/user/kyscorp/perl/counter]
to the path to your CGI directory on your server in which you put the counter.pl
script.
- Modify the 4th line of the script, put the URL to the directory containing the
counter.pl instead of www.kyscorp.com/perl
- Modify the 5th line to "no" instead of "yes" if you want to count the same
user many times.
- Save your file with the extention .pl otherwise
the server won't 'understand' and execute the script.
- Create graphics from 0 to 9, name them 0.gif, 1.gif.......... 9.gif, or
just use those we are offering.
- Create a new file data.txt just write 0 (zero) in it and save it, this will be
the file containing the number of your visitors ;).
- Upload all the files to your server with an FTP client and CHMOD counter.pl to 755 (or
777 if it won't work) and data.txt and gfx to 777.
- Use Server Side includes (SSI) to include the counter in the pages of your choice,
use the following synthax : <!--#exec cgi="cgi-bin/counter.pl"-->
in this case counter.pl is the name of your file and cgi-bin is the directory
containing it, if your page and the counter.pl are in the same directory just use
this <!--#exec cgi="counter.pl"-->
- Save the pages in which you included the counter.pl via SSI with the extention .shtml
to indicate to your server that these pages have a script inside that must be
executed.
- That is all folks !
Our Gfx:
Submitted by : lept || Copyright Notice: Matt Wright || Graphics © KYSCORP.COM