Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

download counter

Posted on 1997-11-01
12
Medium Priority
?
162 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 600 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
New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This tutorial will give you a fast look what you can do with WhizBase. I expect you already know how to work with HTML at least, and that you understand the basics of the internet and how the internet works. WhizBase is a server-s…
This article will show, step by step, how to integrate R code into a R Sweave document
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

636 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