Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help on counter script!

Status
Not open for further replies.
Sep 17, 2008
2
DK
Hi
I have a simple perl script to count hits on my website.
But now I need to develop bit more. so I have few questions.
I need to call the scripts with a variable as follows.

visible='yes' - show the counter
visible='no' - hide the counter

also I need to have the option as "not count me" so my visits will not be counted. I may need to put my IP in the script.
also when I have the counter in frame pages the counter count multiple times. but this should be done only once, even if I have the counter called in multiple pages in the frame. is it possible?

<!--#exec cgi="./cgi-bin/counter5.pl?visible=xx" -->

#!/usr/bin/perl -w


use warnings;
use strict;
use CGI qw/:standard/;

my $file = "counter.dat";

open( LOG, "$file" ) or die "Cannot open file $!";
my $cnt = <LOG>;
close(LOG);

++$cnt;

open( LOG, "> $file" ) or die "Cannot open file $!";
print LOG $cnt;
close(LOG);
print STDOUT "Content-type: text/html\n\n";
print "$cnt";

Thank you.
 
You can get the value of a parameter by using the "param" function that you've already imported from the CGI module:
Code:
my $visible = param( 'visible' );

In order to get any sort of reliability with your counter, you really need to introduce file locking. The way it is at present, it will not count all hits and may even reset itself every now and again.
 
I am pretty sure the exec tag does not pass paramters to programs, you need to use the virtual tag, something like:

<!--#include virtual=""./cgi-bin/counter5.pl?visible=xx"-->

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
The exec tag should be written
[tt]<!--#exec cmd="./cgi-bin/counter5.pl xx" -->[/tt]
and your script will have access to the argument xx in the array [tt]@ARGV[/tt] and will interpret it as needed.


Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
I can confirm the exec tag does pass params to the perl script
Code:
<!--#EXEC CGI="/cgi-bin/menu.pl?ID=1" -->
works fine for me!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Instead of
Code:
open( LOG, "$file" ) or die "Cannot open file $!";
my $cnt = <LOG>;
close(LOG);

++$cnt;

open( LOG, "> $file" ) or die "Cannot open file $!";
print LOG $cnt;
close(LOG);

You can do this
Code:
#  +<   Read first, then write
#  +>   Write first, then read
#  +>>  Append first, then read

open ( LOG, "+< $file" ) or die "Cannot open file $!";
   my $cnt = <LOG>;
   $cnt++;
   seek ( LOG, 0, 0 );
   print LOG $cnt;
close ( LOG );
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top