Current location: Hot Scripts Forums » Programming Languages » Perl » CGI Counter Help


CGI Counter Help

Reply
  #1 (permalink)  
Old 05-24-09, 06:23 AM
moreofahtmlguy's Avatar
moreofahtmlguy moreofahtmlguy is offline
New Member
 
Join Date: May 2009
Location: Bristol
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
CGI Counter Help

I've found and adapted a CGI counter script for my site but I want it to not count me when I go to edit the page. I have got that working but the counter does not show the page counts any longer, instead it just shows 0000. I checked it worked on another computer and it counts. I am pretty new to Perl/CGI so help would be greatly appreciated . I'll highlight the area that checks the IP to see if it is me. Here is the script:

#!/usr/bin/perl
$minLen = 7;

#This is absolute path of the directory where the counter files are stored. Must be global-writeable.
$counterdir = "/home/www/theotherside.awardspace.co.uk/counter/";

$lockWait = 5;
$ip = "91.104.208.196";

&ReadParse;
&initialize;
&loadbitmaps;
✓
&generateBitmap;
&writeBitmap;
exit(0);

sub writeBitmap {
print "Content-type: image/x-xbitmap\n\n";
print "#define count_width ",$len*8,"\n#define count_height 16\n";
print "static char count_bits[] = {\n";
for($i = 0; $i < ($#bytes + 1); $i++) {
print "0x$bytes[$i]";
if ($i != $#bytes) {
print ",";
if (($i+1) % 7 == 0) {
print "\n" ;
}
}
}
print "};\n";
}

sub generateBitmap {
$count = $totalReads;
@bytes = ();
$len = length($count) > $minLen ? length($count) : $minLen;
$outtext = sprintf("%0${len}d",$count);
for ($i = 0; $i < $len*3; $i++ ) {
push(@bytes,"ff");
}
for ($y=0; $y < 10; $y++) {
for ($x=0; $x < $len; $x++) {
push(@bytes,substr($chars{substr($outtext,$x,1)},$ y*3,2));
}
}
for ($i = 0; $i < $len*3; $i++ ) {
push(@bytes,"ff");
}
}


sub initialize {
if ($#in < 0) {
$scriptURL = "http://" . $ENV{'SERVER_NAME'} . ":" . $ENV{'SERVER_PORT'} . $ENV{'SCRIPT_NAME'};
print ("Content-type: text/html\n\n");
print ("<HTML><HEAD><TITLE>Visitor counter</TITLE></HEAD>\n");
print ("<BODY><h1>counter.xbm</h1>To put a visitor counter like this \n");
print ("<IMG SRC=\"$scriptURL?cgicount\">\n");
print ("on your web page, put an image tag in your HTML document like:<br>\n");
print ("&lt;IMG SRC=\"$scriptURL?unique_id\"&gt;<br>\n");
print ("where \"unique_id\" is a unique identifier for your counter.<p>\n");
print ("When creating a new counter you can set the starting value by adding a count parameter to the URL. So if the counter \"qwerty\" didn't exist, accessing the URL<br>\n");
print ("$scriptURL?qwerty&COUNT=1234<br>\n");
print ("will create a new counter called \"qwerty\" with its counter set to 1234.<br>The counter can only be set when creating a new counter.\n");
print ("<hr></BODY></HTML>\n");
exit;
}
else {
$countername = $in[0];
$countername =~ s/\W//g;
$counterFile = $counterdir . $countername;
if (!( -f $counterFile )) {
$start = $in{'COUNT'} -1;
open(CNTRFILE,">$counterFile");
print CNTRFILE "$start\n";
close(CNTRFILE);
}
}
}

######################################
sub check {
if ($ENV{'REMOTE_ADDR'} ne $ip) {
&incrementCount;
} else {
return;
}
}
######################################

sub incrementCount {
if (&lockFile == 1) {
$count = "0";
return;
}
&incrementTotalReads;
&unlockFile;
}

sub unlockFile {
unlink("$counterFile.lock");
}

sub lockFile {
$lockCount = 0;
while (-f "$counterFile.lock") {
if ($lockCount > $lockWait) {
$count = 0;
return 1;
}
sleep 1;
$lockCount++;
}
open(LOCK,">$counterFile.lock") || die("Can't open $counterFile.lock: $!\n");
return 0;
}

sub incrementTotalReads {
if (-e $counterFile) {
open(COUNT,"$counterFile") || die("Can't open $counterFile: $!\n");
}
$totalReads = <COUNT>;
chop $totalReads;
close(COUNT);
$totalReads++;
open(COUNT,">$counterFile") || die "$0: can\'t open $counterFile: $!\n";
print (COUNT "$totalReads\n");
close(COUNT);
}

sub loadbitmaps {
$chars{'0'} = "c3 99 99 99 99 99 99 99 99 c3";
$chars{'1'} = "cf c7 cf cf cf cf cf cf cf c7";
$chars{'2'} = "c3 99 9f 9f cf e7 f3 f9 f9 81";
$chars{'3'} = "c3 99 9f 9f c7 9f 9f 9f 99 c3";
$chars{'4'} = "cf cf c7 c7 cb cb cd 81 cf 87";
$chars{'5'} = "81 f9 f9 f9 c1 9f 9f 9f 99 c3";
$chars{'6'} = "c7 f3 f9 f9 c1 99 99 99 99 c3";
$chars{'7'} = "81 99 9f 9f cf cf e7 e7 f3 f3";
$chars{'8'} = "c3 99 99 99 c3 99 99 99 99 c3";
$chars{'9'} = "c3 99 99 99 99 83 9f 9f cf e3";
}

# Perl Routines to Manipulate CGI input
# Copyright 1993 Steven E. Brenner
sub ReadParse {
if (@_) {
local (*in) = @_;
}

local ($i, $loc, $key, $val);

# Read in text
if ($ENV{'REQUEST_METHOD'} eq "GET") {
$in = $ENV{'QUERY_STRING'};
} elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
for ($i = 0; $i < $ENV{'CONTENT_LENGTH'}; $i++) {
$in .= getc;
}
}

@in = split(/&/,$in);

foreach $i (0 .. $#in) {
# Convert plus's to spaces
$in[$i] =~ s/\+/ /g;

# Convert %XX from hex numbers to alphanumeric
$in[$i] =~ s/%(..)/pack("c",hex($1))/ge;

# Split into key and value.
$loc = index($in[$i],"=");
$key = substr($in[$i],0,$loc);
$val = substr($in[$i],$loc+1);
$in{$key} .= '\0' if (defined($in{$key})); # \0 is the multiple separator
$in{$key} .= $val;
}

return 1;
}

Last edited by moreofahtmlguy; 05-24-09 at 06:25 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 05-27-09, 03:40 AM
moreofahtmlguy's Avatar
moreofahtmlguy moreofahtmlguy is offline
New Member
 
Join Date: May 2009
Location: Bristol
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
No replies shame, onto a new problem...

Last edited by moreofahtmlguy; 05-27-09 at 03:43 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 05-27-09, 03:42 AM
moreofahtmlguy's Avatar
moreofahtmlguy moreofahtmlguy is offline
New Member
 
Join Date: May 2009
Location: Bristol
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by moreofahtmlguy View Post
I've found and adapted a CGI counter script for my site but I want it to not count me when I go to edit the page. I have got that working but the counter does not show the page counts any longer, instead it just shows 0000. I checked it worked on another computer and it counts. I am pretty new to Perl/CGI so help would be greatly appreciated . I'll highlight the area that checks the IP to see if it is me. Here is the script:

#!/usr/bin/perl
$minLen = 7;

#This is absolute path of the directory where the counter files are stored. Must be global-writeable.
$counterdir = "/home/www/theotherside.awardspace.co.uk/counter/";

$lockWait = 5;
$ip = "91.104.208.196";

&ReadParse;
&initialize;
&loadbitmaps;
&check;
&generateBitmap;
&writeBitmap;
exit(0);

sub writeBitmap {
print "Content-type: image/x-xbitmap\n\n";
print "#define count_width ",$len*8,"\n#define count_height 16\n";
print "static char count_bits[] = {\n";
for($i = 0; $i < ($#bytes + 1); $i++) {
print "0x$bytes[$i]";
if ($i != $#bytes) {
print ",";
if (($i+1) % 7 == 0) {
print "\n" ;
}
}
}
print "};\n";
}

sub generateBitmap {
$count = $totalReads;
@bytes = ();
$len = length($count) > $minLen ? length($count) : $minLen;
$outtext = sprintf("%0${len}d",$count);
for ($i = 0; $i < $len*3; $i++ ) {
push(@bytes,"ff");
}
for ($y=0; $y < 10; $y++) {
for ($x=0; $x < $len; $x++) {
push(@bytes,substr($chars{substr($outtext,$x,1)},$ y*3,2));
}
}
for ($i = 0; $i < $len*3; $i++ ) {
push(@bytes,"ff");
}
}


sub initialize {
if ($#in < 0) {
$scriptURL = "http://" . $ENV{'SERVER_NAME'} . ":" . $ENV{'SERVER_PORT'} . $ENV{'SCRIPT_NAME'};
print ("Content-type: text/html\n\n");
print ("<HTML><HEAD><TITLE>Visitor counter</TITLE></HEAD>\n");
print ("<BODY><h1>counter.xbm</h1>To put a visitor counter like this \n");
print ("<IMG SRC=\"$scriptURL?cgicount\">\n");
print ("on your web page, put an image tag in your HTML document like:<br>\n");
print ("&lt;IMG SRC=\"$scriptURL?unique_id\"&gt;<br>\n");
print ("where \"unique_id\" is a unique identifier for your counter.<p>\n");
print ("When creating a new counter you can set the starting value by adding a count parameter to the URL. So if the counter \"qwerty\" didn't exist, accessing the URL<br>\n");
print ("$scriptURL?qwerty&COUNT=1234<br>\n");
print ("will create a new counter called \"qwerty\" with its counter set to 1234.<br>The counter can only be set when creating a new counter.\n");
print ("<hr></BODY></HTML>\n");
exit;
}
else {
$countername = $in[0];
$countername =~ s/\W//g;
$counterFile = $counterdir . $countername;
if (!( -f $counterFile )) {
$start = $in{'COUNT'} -1;
open(CNTRFILE,">$counterFile");
print CNTRFILE "$start\n";
close(CNTRFILE);
}
}
}

######################################
sub check {
if ($ENV{'REMOTE_ADDR'} ne $ip) {
&incrementCount;
} else {
return;
}
}
######################################

sub incrementCount {
if (&lockFile == 1) {
$count = "0";
return;
}
&incrementTotalReads;
&unlockFile;
}

sub unlockFile {
unlink("$counterFile.lock");
}

sub lockFile {
$lockCount = 0;
while (-f "$counterFile.lock") {
if ($lockCount > $lockWait) {
$count = 0;
return 1;
}
sleep 1;
$lockCount++;
}
open(LOCK,">$counterFile.lock") || die("Can't open $counterFile.lock: $!\n");
return 0;
}

sub incrementTotalReads {
if (-e $counterFile) {
open(COUNT,"$counterFile") || die("Can't open $counterFile: $!\n");
}
$totalReads = <COUNT>;
chop $totalReads;
close(COUNT);
$totalReads++;
open(COUNT,">$counterFile") || die "$0: can\'t open $counterFile: $!\n";
print (COUNT "$totalReads\n");
close(COUNT);
}

sub loadbitmaps {
$chars{'0'} = "c3 99 99 99 99 99 99 99 99 c3";
$chars{'1'} = "cf c7 cf cf cf cf cf cf cf c7";
$chars{'2'} = "c3 99 9f 9f cf e7 f3 f9 f9 81";
$chars{'3'} = "c3 99 9f 9f c7 9f 9f 9f 99 c3";
$chars{'4'} = "cf cf c7 c7 cb cb cd 81 cf 87";
$chars{'5'} = "81 f9 f9 f9 c1 9f 9f 9f 99 c3";
$chars{'6'} = "c7 f3 f9 f9 c1 99 99 99 99 c3";
$chars{'7'} = "81 99 9f 9f cf cf e7 e7 f3 f3";
$chars{'8'} = "c3 99 99 99 c3 99 99 99 99 c3";
$chars{'9'} = "c3 99 99 99 99 83 9f 9f cf e3";
}

# Perl Routines to Manipulate CGI input
# Copyright 1993 Steven E. Brenner
sub ReadParse {
if (@_) {
local (*in) = @_;
}

local ($i, $loc, $key, $val);

# Read in text
if ($ENV{'REQUEST_METHOD'} eq "GET") {
$in = $ENV{'QUERY_STRING'};
} elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
for ($i = 0; $i < $ENV{'CONTENT_LENGTH'}; $i++) {
$in .= getc;
}
}

@in = split(/&/,$in);

foreach $i (0 .. $#in) {
# Convert plus's to spaces
$in[$i] =~ s/\+/ /g;

# Convert %XX from hex numbers to alphanumeric
$in[$i] =~ s/%(..)/pack("c",hex($1))/ge;

# Split into key and value.
$loc = index($in[$i],"=");
$key = substr($in[$i],0,$loc);
$val = substr($in[$i],$loc+1);
$in{$key} .= '\0' if (defined($in{$key})); # \0 is the multiple separator
$in{$key} .= $val;
}

return 1;
}
Forget the above, I no longer want it to not count me. Instead I have a different counter script and I want to have it output the counter using the IMG tag in a html page. My server does not support SSI so I can't even consider that. Here is the script:

#!/usr/bin/perl

#variables
$counterdir = "/home/www/theotherside.awardspace.co.uk/counter/";
$countername = "count2";
$counterFile = $counterdir . $countername;
$image="/counter/digits2";

#read counter file

open(COUNT,"$counterFile") || die("Can't open $counterFile: $!\n");
$data = <COUNT>;
close(COUNT);
$data++;
open(COUNT,">$counterFile") || die "$0: can\'t open $counterFile: $!\n";
print (COUNT "$data");
$data2 = $data;
close(COUNT);

# Determine counter width
$num=$length=length($data);

# Create Array
while ($num>0)
{
$CHAR{$num} = chop($data);
$num--;
}

# Output counter
$j="1";
print ("Content-type: text/html\n\n");
while ($j<=$length)
{
print ("<img src=\"$image/$CHAR{$j}.gif\">");
$j++;
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 06-03-09, 08:35 AM
winracer's Avatar
winracer winracer is offline
Newbie Coder
 
Join Date: Jul 2008
Location: Georgia
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
I use this
Code:
print qq~
<style>
.counter
{
 border:4px groove #08f;
 padding:5px; /* Adjust this value to change padding around numbers. */
 float:right;
 margin-right:10px; /* Adjust this value to position from right. */
 margin-top:10px; /* Adjust this value to position from top. */
 margin-bottom:10px; /* Adjust this value to position from bottom */

}
.number
{
 float:left;
 }
</style>
~;
$countermain = $upload_path."/"."mainpage.txt";
$counter = "77684"; ### You can seed any numeric value into $counter. Default is 0. ###
if(-e "$countermain")
{
 open counter, "< $countermain";
 $count = <counter>;
 $counter = $count+1;
 close counter;
 open counter, "+> $countermain";
 printf counter $counter;
 close counter;
 }
else
{
 open counter, "+> $countermain";
 printf counter $counter;
 close counter;
 chmod(0666, $countermain);
 }
$pos=0;

$letter_style = "WhiteOdometer";       ### Options: BlackOdometer, imagesbigger, WhiteOdometer ###

@numbers = ("0.gif","1.gif","2.gif","3.gif","4.gif","5.gif","6.gif","7.gif","8.gif","9.gif");
$len = length $counter;
for($i=0;$i<$len;$i++)
{
 if(substr($counter, $i, 1) == 0)
 {
  substr($counter, $i, 1) = "\0";
  }
 }
print qq~ <div class="counter"> ~;
while($str = substr($counter, $pos, 1))
{
 print "<div class='number'><img src='images/$letter_style/$numbers[$str]' border='0'></div>";
 $pos++;
 }
print qq~
</div>
~;

}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
simple counter Lunar Script Requests 1 11-28-06 04:07 AM
Transfer values from JavaScript to cgi agent186 Perl 3 07-26-06 12:40 PM
CGI or PHP better? mhs12grade1992 PHP 11 02-10-05 09:01 PM
Counter script (to offer free counter service) basara55 Script Requests 0 06-29-04 09:48 PM


All times are GMT -5. The time now is 09:59 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.