Solved

download counter

Posted on 1997-11-01
12
157 Views
Last Modified: 2013-12-25
How do you count how many times a file is downloaded from your homepage?
0
Comment
Question by:hh
  • 6
  • 3
  • 2
  • +1
12 Comments
 
LVL 5

Expert Comment

by:julio011597
ID: 1831026
Take the link that points to the file to download, and make that link point to a CGI instead.

The CGI should just:

1. work as a counter, i.e. increment a value in some file;
2. redirect the client browser to the file to download.

If you need the code, you should specify OS and WebServer you are on.
BTW, i could give you some C code.

Rgds, julio
0
 

Accepted Solution

by:
dankarran earned 200 total points
ID: 1831027
Try this script: (put it into a cgi-bin directory in your site.
(you will need to change some of the variables)

#!/usr/bin/perl

#Call the file using <a href=/yourusername/cgi-bin/filedl.cgi?filename.zip>

unless ($ENV{'QUERY_STRING'}) {
exit;
}

$counter = $ENV{'QUERY_STRING'} . ".cnt";
unless (-e $counter) {
open (COUNT,">$counter");
print COUNT "0\n";
close COUNT;
}

$count = 0;
open (COUNTER,$counter) || die "Content-type:text/plain\n\nCan't open $counter!\n";
$count = <COUNTER>;
close COUNTER;
$count++;
open (COUNTER,">$countname");
print COUNTER "$count\n";
close COUNTER;

print "Location: /yourusername/filename.zip\n\n";

exit;
0
 

Author Comment

by:hh
ID: 1831028
to julio

the OS is UNIX and the adress is http://sdf.lonestar.org/~hh
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Expert Comment

by:dankarran
ID: 1831029
Ok, I spooted a fault in the previous script, so use this instead....


#!/usr/bin/perl

#Call the file using <a href=/~hh/cgi-bin/filedl.cgi?filename.zip>
unless ($ENV{'QUERY_STRING'}) {
print "Location: $ENV{'HTTP_REFERER'}\n\n";
exit;
}
$counter = $ENV{'QUERY_STRING'} . ".cnt";
unless (-e $counter) {
open (COUNT,">$counter");
print COUNT "0\n";
close COUNT;
}
$count = 0;
open (COUNTER,$counter) || die "Content-type:text/plain\n\nCan't open
$counter!\n";
$count = <COUNTER>;
close COUNTER;
$count++;
open (COUNTER,">$countname");
print COUNTER "$count\n";
close COUNTER;
print "Location: http://sdf.lonestar.org/~hh/$ENV{'QUERY_STRING'}\n\n";
exit;
0
 
LVL 84

Expert Comment

by:ozo
ID: 1831030
That looks like it could be dangerous to call that with something like
/cgi-bin/filedl.cgi?|rm+filedl.cgi;
(and the die will print to STDERR, not STDOUT,
and $countname seems to be undefined)
0
 

Expert Comment

by:dankarran
ID: 1831031
Ok, some fixes - but I don't know about that /cgi-bin/filedl.cgi?|rm+filedl.cgi; loophole, how could I fix that ozo?
I don't know why that $countname wasn't picked up when I tested it - usually when I do something like that, I get an error 500. And it wasn't thrown up when I tested it through the command line.


Dan


#!/usr/bin/perl
#Call the file using <a href=/~hh/cgi-bin/filedl.cgi?filename.zip>
unless ($ENV{'QUERY_STRING'}) {
print "Location: $ENV{'HTTP_REFERER'}\n\n";
exit;
}
$counter = $ENV{'QUERY_STRING'} . ".cnt";
unless (-e $counter) {
open (COUNT,">$counter");
print COUNT "0\n";
close COUNT;
}
$count = 0;
open (COUNTER,$counter);
$count = <COUNTER>;
close COUNTER;
$count++;
open (COUNTER,">$counter");
print COUNTER "$count\n";
close COUNTER;
print "Location: http://sdf.lonestar.org/~hh/$ENV{'QUERY_STRING'}\n\n";
exit;
0
 

Expert Comment

by:dankarran
ID: 1831032
Ok, some fixes - but I don't know about that /cgi-bin/filedl.cgi?|rm+filedl.cgi; loophole, how could I fix that ozo?
I don't know why that $countname wasn't picked up when I tested it - usually when I do something like that, I get an error 500. And it wasn't thrown up when I tested it through the command line.


Dan


#!/usr/bin/perl
#Call the file using <a href=/~hh/cgi-bin/filedl.cgi?filename.zip>
unless ($ENV{'QUERY_STRING'}) {
print "Location: $ENV{'HTTP_REFERER'}\n\n";
exit;
}
$counter = $ENV{'QUERY_STRING'} . ".cnt";
unless (-e $counter) {
open (COUNT,">$counter");
print COUNT "0\n";
close COUNT;
}
$count = 0;
open (COUNTER,$counter);
$count = <COUNTER>;
close COUNTER;
$count++;
open (COUNTER,">$counter");
print COUNTER "$count\n";
close COUNTER;
print "Location: http://sdf.lonestar.org/~hh/$ENV{'QUERY_STRING'}\n\n";
exit;
0
 
LVL 84

Expert Comment

by:ozo
ID: 1831033
#!/usr/bin/perl -w
will catch errors like $countname when testing through the command line.
#!/usr/bin/perl -w
use strict;
will catch even more.

open (COUNTER,"<$counter") or (print "Content-type:text/plain\n\nCan't open counter!\n" and die);
might prevent filedl.cgi?|rm+filedl.cgi;
from invoking shell commands, but not from opening strange files
(counts could also be lost if two people call the file at the samr time,
but that may not be as serious a problem)
0
 

Expert Comment

by:dankarran
ID: 1831034
what exactly does that rm thingie do?
0
 
LVL 84

Expert Comment

by:ozo
ID: 1831035
looking up the open command in perlfunc, we see that if the filename begins with a |,
the filename is interpreted as a command to which output is to be piped, so
open (COUNTER,"|rm filedl.cgi;.cnt");
may try to execute a command which we'd rather not execute.
0
 

Author Comment

by:hh
ID: 1831036
well What is the final script
0
 

Expert Comment

by:dankarran
ID: 1831037
ozo, could you correct the script to stop the rm thingie, then post it back?

Thanks for your help,
Dan
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question