##################################################################
### (More Accurate) Record Hit Counter and Top Ten Hits List ###
### originally by leisurelee and JPDeni ###
### redesigned by TheFew (AJ) ###
### arusso@thefew.com ###
### see in action: http://www.thefew.com/cgi-bin/db/db.cgi ###
### ###
### Modified: 21 Apr 2000 - by leisurelee and JPDeni ###
### Modified: 05 Apr 2000 - by TheFew (AJ) ###
##################################################################
Well, it took me a few hours, but I got all the bugs worked out. This is partly a continuation of thread www.gossamer-threads.com/scripts/forum/resources/Forum12/HTML/002847.html .
The difference in the 05 Apr 2000 version from the original is an
iplog.txt file is created in the "temp" directory and when a record is hit DBMan writes the line:
IP Address|UNIX Date|$db_key
to iplog.txt. If within a specified time the same record is hit by the same IP Address it will NOT record a hit. After the specified time, the line will be deleted, and that IP Address can hit the record and count another hit. This keeps cheaters from pushing a record to the top.
How to set up this MOD:
Create a directory within your DBMan directory. Name the new directory "temp". Set the permissions on the directory to 777 (-rwxrwxrwx)
###############################################################
# default.cfg #
#-------------------------------------------------------------#
# Add the following to your .cfg file: #
###############################################################
### In the # File and URL's section add:
$counter_dir = $db_script_path . "/temp";
### After $auth_logoff = "http://www.yourdomain.com"; add:
# 3600 - One Hour (Recomended)
$ipupdate = "3600"; ### Added for time interval###############################################################
# html.pl #
#-------------------------------------------------------------#
# In 'sub html_record' (or 'sub html_record_long', if you're #
# using the short/long display mod): #
###############################################################
### MOVE the following line
### below this line
### AND add the following after the moved line
###--- What you will end up with at the top of sub html_record(_long) ---####
($db_auto_generate and print &build_html_record(%rec) and return);
&top10;
### Reference $countup wherever you want to show the # of hits made to the record
### To include link to a top ten list of the most visited records, add the following subroutine:
#-------------------------------------------------------------
opendir (TEMPDIR, "$counter_dir");
@files = readdir(TEMPDIR); # Read in list of files in directory..
closedir (TEMPDIR);
FILE: foreach $file (@files) {
next if ($file =~ /^\./); # Skip "." and ".." entries..
next if ($file =~ /^index/); # Skip index.htm type files..
next if ($file =~ /^iplog/); # Skip iplog.txt file..
open (COUNTER, "<$counter_dir/$file");
$count{$file} = int(<COUNTER> );
close COUNTER;
}
foreach $key (sort {$count{$b} <=> $count{$a} } keys %count) { push (@top_ten,$key); }
&html_print_headers;
print qq|
<html>
<head><title>$html_title: Top Ten Records.</title></head>
<BODY>
header html code goes here
|;
for ($i=0;$i<10 ;$i++) {
$toparray .= "$top_ten[$i],";
}
chop $toparray;
foreach my $toplist (split(/,/,$toparray)) {
&html_record(&get_record($toplist)); ## See below if you're using short/long display mod
}
print qq|footer html code goes here|;
&html_footer;
print qq|</body></html>|;
}###############################################################
# If you're using the short/long display mod, look above #
# at the commented line and change as appropriate #
###############################################################
### Instead of:
#### Use the following, and change $rec{'FieldName'} to match your database.
print qq|
<a href="$db_script_link_url&$db_key=$rec{$db_key}&view_records=1&ww=1">$rec{'FieldName'}</a>
|;
### In 'sub html_footer', add
###############################################################
# db.cgi #
#-------------------------------------------------------------#
# Delete (or comment out) this line in sub get_record: #
###############################################################
### In 'sub main', after
### add
### Also, add new subroutine:
#-----------------------------------------------------------
my ($db_key) = $_[0];
$count_up= "$counter_dir/$db_key";
$iptime = time();
$remote = $ENV{'REMOTE_ADDR'};
$cheat = 0;
if (-e "$counter_dir/iplog.txt") {
open (iplog, "+<$counter_dir/iplog.txt");
if ($db_use_flock) { flock(iplog, 2); }
@ipdata = <iplog>;
seek (iplog, 0, 0);
truncate (iplog,0);
foreach $log (@ipdata) {
@ips = split(/\|/, $log);
$iptotal = $iptime - $ips[1];
if (($ips[0] == $remote) && ($ips[2] == $db_key) && ($iptotal < $ipupdate)) { $cheat = 1; }
if ($iptotal < $ipupdate) { print iplog "$ips[0]|$ips[1]|$ips[2]\n"; }
}
unless ($cheat) { print iplog "$remote|$iptime|$db_key\n"; }
close (iplog);
} else {
open(newip, ">$counter_dir/iplog.txt");
print newip "$remote|$iptime|$db_key\n";
close (newip);
}
if (!$cheat) {
open (COUNT, "$count_up");
if ($db_use_flock) { flock(COUNT, 2); }
$countup = <COUNT>;
$countup++;
close COUNT;
open (COUNT, ">$count_up");
if ($db_use_flock) { flock(COUNT, 2); }
print COUNT $countup;
close COUNT;
} else {
open (COUNT, "<$count_up");
if ($db_use_flock) { flock(COUNT, 2); }
$countup = <COUNT>;
close COUNT;
}
}
That's it! To see it in action, go to:
www.thefew.com/cgi-bin/db/db.cgi
You will automatically be logged in as 'default'. From the menu, choose search and do a search. You'll get the short list. Click on a last name to get the long form. You have just recorded a hit and you will see how many hits there have been on that record. Go back to the short list and click on the record again. It should not record a hit. I have the '$ipupdate' time set to 60 seconds. If you go back to the short list, wait 60 seconds, then click on the record again, you'll see the # hits go up. On that page, go to the Jump To: box and choose 'Top 10' to see the Top 10 list with view counts and alternating colors (did not include these 2 things in this MOD - will be happy to provide to code for it on request).
Any questions please reply to this thread.
Semper Fi,
AJ
[This message has been edited by TheFew (edited May 06, 2000).]
[This message has been edited by TheFew (edited May 06, 2000).]
### (More Accurate) Record Hit Counter and Top Ten Hits List ###
### originally by leisurelee and JPDeni ###
### redesigned by TheFew (AJ) ###
### arusso@thefew.com ###
### see in action: http://www.thefew.com/cgi-bin/db/db.cgi ###
### ###
### Modified: 21 Apr 2000 - by leisurelee and JPDeni ###
### Modified: 05 Apr 2000 - by TheFew (AJ) ###
##################################################################
Well, it took me a few hours, but I got all the bugs worked out. This is partly a continuation of thread www.gossamer-threads.com/scripts/forum/resources/Forum12/HTML/002847.html .
The difference in the 05 Apr 2000 version from the original is an
iplog.txt file is created in the "temp" directory and when a record is hit DBMan writes the line:
IP Address|UNIX Date|$db_key
to iplog.txt. If within a specified time the same record is hit by the same IP Address it will NOT record a hit. After the specified time, the line will be deleted, and that IP Address can hit the record and count another hit. This keeps cheaters from pushing a record to the top.
How to set up this MOD:
Create a directory within your DBMan directory. Name the new directory "temp". Set the permissions on the directory to 777 (-rwxrwxrwx)
###############################################################
# default.cfg #
#-------------------------------------------------------------#
# Add the following to your .cfg file: #
###############################################################
### In the # File and URL's section add:
Code:
# Full Path and File name of the record view count directory. $counter_dir = $db_script_path . "/temp";
### After $auth_logoff = "http://www.yourdomain.com"; add:
Code:
# How many seconds to remove the old ip addresses for record view count? # 3600 - One Hour (Recomended)
$ipupdate = "3600"; ### Added for time interval
# html.pl #
#-------------------------------------------------------------#
# In 'sub html_record' (or 'sub html_record_long', if you're #
# using the short/long display mod): #
###############################################################
### MOVE the following line
Code:
($db_auto_generate and print &build_html_record(%rec) and return);Code:
my (%rec) = @_;Code:
&top10($rec{$db_key});###--- What you will end up with at the top of sub html_record(_long) ---####
Code:
my (%rec) = @_; ($db_auto_generate and print &build_html_record(%rec) and return);
&top10;
### Reference $countup wherever you want to show the # of hits made to the record
### To include link to a top ten list of the most visited records, add the following subroutine:
Code:
sub html_topten { #-------------------------------------------------------------
opendir (TEMPDIR, "$counter_dir");
@files = readdir(TEMPDIR); # Read in list of files in directory..
closedir (TEMPDIR);
FILE: foreach $file (@files) {
next if ($file =~ /^\./); # Skip "." and ".." entries..
next if ($file =~ /^index/); # Skip index.htm type files..
next if ($file =~ /^iplog/); # Skip iplog.txt file..
open (COUNTER, "<$counter_dir/$file");
$count{$file} = int(<COUNTER> );
close COUNTER;
}
foreach $key (sort {$count{$b} <=> $count{$a} } keys %count) { push (@top_ten,$key); }
&html_print_headers;
print qq|
<html>
<head><title>$html_title: Top Ten Records.</title></head>
<BODY>
header html code goes here
|;
for ($i=0;$i<10 ;$i++) {
$toparray .= "$top_ten[$i],";
}
chop $toparray;
foreach my $toplist (split(/,/,$toparray)) {
&html_record(&get_record($toplist)); ## See below if you're using short/long display mod
}
print qq|footer html code goes here|;
&html_footer;
print qq|</body></html>|;
}
# If you're using the short/long display mod, look above #
# at the commented line and change as appropriate #
###############################################################
### Instead of:
Code:
&html_record(&get_record($toplist));Code:
%rec = (&get_record($toplist)); print qq|
<a href="$db_script_link_url&$db_key=$rec{$db_key}&view_records=1&ww=1">$rec{'FieldName'}</a>
|;
### In 'sub html_footer', add
Code:
print qq!|<A HREF="$db_script_link_url&topten=1">Top Ten</A> ! if ($per_view);###############################################################
# db.cgi #
#-------------------------------------------------------------#
# Delete (or comment out) this line in sub get_record: #
###############################################################
Code:
($restricted = 1) if ($auth_modify_own and !$per_admin);### In 'sub main', after
Code:
if ($in{'add_form'}) { if ($per_add) { &html_add_form; } else { &html_unauth; } }Code:
elsif ($in{'topten'}) { if ($per_view) { &html_topten; } else { &html_unauth; } }### Also, add new subroutine:
Code:
sub top10 { #-----------------------------------------------------------
my ($db_key) = $_[0];
$count_up= "$counter_dir/$db_key";
$iptime = time();
$remote = $ENV{'REMOTE_ADDR'};
$cheat = 0;
if (-e "$counter_dir/iplog.txt") {
open (iplog, "+<$counter_dir/iplog.txt");
if ($db_use_flock) { flock(iplog, 2); }
@ipdata = <iplog>;
seek (iplog, 0, 0);
truncate (iplog,0);
foreach $log (@ipdata) {
@ips = split(/\|/, $log);
$iptotal = $iptime - $ips[1];
if (($ips[0] == $remote) && ($ips[2] == $db_key) && ($iptotal < $ipupdate)) { $cheat = 1; }
if ($iptotal < $ipupdate) { print iplog "$ips[0]|$ips[1]|$ips[2]\n"; }
}
unless ($cheat) { print iplog "$remote|$iptime|$db_key\n"; }
close (iplog);
} else {
open(newip, ">$counter_dir/iplog.txt");
print newip "$remote|$iptime|$db_key\n";
close (newip);
}
if (!$cheat) {
open (COUNT, "$count_up");
if ($db_use_flock) { flock(COUNT, 2); }
$countup = <COUNT>;
$countup++;
close COUNT;
open (COUNT, ">$count_up");
if ($db_use_flock) { flock(COUNT, 2); }
print COUNT $countup;
close COUNT;
} else {
open (COUNT, "<$count_up");
if ($db_use_flock) { flock(COUNT, 2); }
$countup = <COUNT>;
close COUNT;
}
}
That's it! To see it in action, go to:
www.thefew.com/cgi-bin/db/db.cgi
You will automatically be logged in as 'default'. From the menu, choose search and do a search. You'll get the short list. Click on a last name to get the long form. You have just recorded a hit and you will see how many hits there have been on that record. Go back to the short list and click on the record again. It should not record a hit. I have the '$ipupdate' time set to 60 seconds. If you go back to the short list, wait 60 seconds, then click on the record again, you'll see the # hits go up. On that page, go to the Jump To: box and choose 'Top 10' to see the Top 10 list with view counts and alternating colors (did not include these 2 things in this MOD - will be happy to provide to code for it on request).
Any questions please reply to this thread.
Semper Fi,
AJ
[This message has been edited by TheFew (edited May 06, 2000).]
[This message has been edited by TheFew (edited May 06, 2000).]