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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie - can I count the hits to a cgi page?

Status
Not open for further replies.

bazil

Programmer
Jan 1, 2001
8
GB
I have a section on my site that is in my cgi bin
Is there any way of tracking the hits to it, for people are bookmarking it and I Don't know how many hits I am getting on it because my log stats only show the last 100 requests for files
 
Designate an area on your system to store a file to count it and then have the cgi scripts add to a number in that file.

#!C:/perl/bin/perl
open (HITCOUNTER, "C:/hits.txt");
@HITCOUNT = <HITCOUNTER>;
close (HITCOUNTER);
$HITS = @HITCOUNT;
$HITS++;
open (HITCOUNTER, &quot;>C:/hits.txt&quot;);
print HITCOUNTER, $HITS;
close (HITCOUNTER);

that will work as your plain old counter. you can then use $HITS later in the script to post it on the page. You can also modify that so that it appends to the file and then it'll act more like a log file than a plain counter.

Steve Kiehl
webmaster@nanovox.com
 
You don't actually have to open and close the file to do that either. You can open it for both at the same time. It might be a little faster. Also since you know it only has a single line in it, you don't really need to worry about the array, and if you don't put a \n on the end of the one record, you don't even need to chomp it.
Code:
open (HITCOUNTER, &quot;+>C:/hits.txt&quot;);
$HITS = <HITCOUNTER>;
$HITS++;
seek(HITCOUNTER,0,0);
print HITCOUNTER $HITS;
close (HITCOUNTER);
Note that there should be a comma in the print statement.

To make this work you'll also have to create the hits.txt file yourself. Just use notepad or whatever to create a file that contains just a 0, no return at the end, and save it.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
I am using <!--#exec cgi="/cgi-bin/counter.pl" --> to call the counter script in my .shtml file. I just cant get it to work. I have other scripts on this page working, a simple Hello World script.

I have tried both code snippets above.

My counter.pl:
#!c:/perl/bin/perl
open (HITCOUNTER, "C:\apache\cgi-bin\hits.txt");
@HITCOUNT = <HITCOUNTER>;
close (HITCOUNTER);
$HITS = @HITCOUNT;
$HITS++;
open (HITCOUNTER, ">C:\apache\cgi-bin\hits.txt");
print HITCOUNTER, $HITS;
close (HITCOUNTER);

Any ideas?

-Dave
 
are you using the correct shebang line for your server?

#!c:/perl/bin/perl

most web servers use:

#!/usr/bin/perl
 
Here is the error from the apache error logs:

Premature end of script headers: d:/apache/cgi-bin/counter.pl

-Dave
 
are you printing the counter to the screen as well as incrementing it? If so you have to print a header first. You could use this:

print "Content-type: text/html\n\n";

then print the counter output


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top