Advertisement

02.01.2008 at 12:55AM PST, ID: 23128967
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Detect click on HREF link?

Tags: Larry Wall, Perl, 5.8, Download Counter HTML Web, Perl, HTML Download Counter Detect HREF click
Hi Experts,

I'm wanting to detect when a visitor downloads a file on my website, so I can do things like: immediately increment counters, etc.
My HTML might be something like this (but I can change it if required):
  <A HREF="MyFile.txt">MyFile.txt<A>
How can I make it so when a user clicks the link it starts up a Perl script, or whatever?

I know I could look at web stats on the site, but I think they are not updated promptly and I'm wondering if there is a more direct method, without having to monitor logs, etc.

Also, how can I get the IP Address of the person who clicks the link?

Thanks...TRS
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: tel2
Solution Provided By: kebabs
Participating Experts: 3
Solution Grade: A
Views: 16
Translate:
Loading Advertisement...
02.01.2008 at 01:11AM PST, ID: 20795150

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.01.2008 at 01:12AM PST, ID: 20795156

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.01.2008 at 01:13AM PST, ID: 20795163

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.01.2008 at 01:22AM PST, ID: 20795214

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.01.2008 at 01:29AM PST, ID: 20795241

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.01.2008 at 01:31AM PST, ID: 20795251

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.01.2008 at 01:37AM PST, ID: 20795279

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.01.2008 at 01:45AM PST, ID: 20795313

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.01.2008 at 02:53AM PST, ID: 20795568

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.01.2008 at 08:36AM PST, ID: 20798047

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.01.2008 at 11:37PM PST, ID: 20803615

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.11.2008 at 04:20PM PST, ID: 20871622

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.11.2008 at 10:20PM PST, ID: 20872815

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.19.2008 at 01:04PM PST, ID: 20932559

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • Automotive
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Displays / Monitors
  • Handhelds / PDAs
  • Components
  • Peripherals
  • Laptops/Notebooks
  • Servers
  • Misc
  • Apple
  • Embedded Hardware
  • Networking Hardware
  • Storage
  • Desktops
  • New Users
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMware
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Virtualization
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • Web Computing
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Consulting
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMware
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Automation
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Web Services
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Web Computing
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Lounge
  • Business Travel
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
  • Automotive
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
02.01.2008 at 01:11AM PST, ID: 20795150
To add on click event use this:
<A onclick="yourfunc()" HREF="MyFile.txt">MyFile.txt<A>
You need to make a function (yourfunc) that make that update for you with ajax.
I advice you to use this jquery. (www.jquery.com)
http://docs.jquery.com/Ajax/jQuery.ajax
Assisted Solution
 
02.01.2008 at 01:12AM PST, ID: 20795156
You can either set up a HTTP request that triggers through a JavaScript onclick event or simply link to the server side script which both does the counter incrementing and outputs the requested file. The IP address can be obtained with $ENV{REMOTE_ADDR} in Perl.
 
02.01.2008 at 01:13AM PST, ID: 20795163
yo_s_canta, that is a bit of an unnecessary burden don't you think? And it requires the client to have JavaScript.
 
02.01.2008 at 01:22AM PST, ID: 20795214
Your solution need also javascript,  you can't do it without js.
That framework will help him in the future, it's very simple to work with that.
look here http://docs.jquery.com/Ajax/load#urldatacallback
It is very simple to do that
1:
2:
3:
4:
5:
$("a").click( 
    function() {
        $.get("count.php", { downloadid: "32345"} );
    }
);
Open in New Window
 
02.01.2008 at 01:29AM PST, ID: 20795241
Thanks guys,

I no nothing about ajax and I don't even know if my web host supports it.  I know it supports Perl & PHP (Perl is my preference).

kebabs, you suggested I could:
> simply link to the server side script which both does the counter incrementing and outputs the requested file.
could you please explain how I might do the above?  I know how to get a Perl script to increment the counter, but how do I get it to start the file download?

Thanks.
 
02.01.2008 at 01:31AM PST, ID: 20795251
1. I proposed two suggestions. The latter one was the preferred one and does not require JS
2. It might be useful in other scenarios, but it is a dependency nonetheless

This is what I suggest:

<a href="getfile.php?file=MyFile.txt">MyFile.txt</a>

getfile.php
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
<?php
 
// using PHP as it is more beginner friendly than Perl
 
// quick and dirty validation
if (!isset($_GET['file'])) exit;
// e.g. only allow text files in same directory
if (preg_match('~(\.\.|/)\.txt$~', $_GET['file'])) exit;
 
// Increment counter, get IP ($_SERVER['REMOTE_ADDR']) etc.
 
// Output contents of requested file
readfile($_GET['file']);
Open in New Window
 
02.01.2008 at 01:37AM PST, ID: 20795279
BTW, I was replying to yo_s_canta.

tel2:
Sorry for using PHP, I didn't see your post until after I wrote it.
You can modify my example to download the requested file by adding:

header('Content-Disposition: attachment; filename="'.$_GET['file'].'"');
header('Content-Length: ' . filesize($_GET['file']));

Here is the modified example:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
<?php
 
// quick and dirty validation
 
if (!isset($_GET['file'])) exit;
$file = $_GET['file'];
 
if (!file_exists($file)) exit;
 
// e.g. only allow text files in same directory
if (preg_match('~(\.\.|/)\.txt$~', $file)) exit;
 
// Increment counter, get IP ($_SERVER['REMOTE_ADDR']) etc.
 
// Output contents of requested file, optionally, use a location header
header('Content-Disposition: attachment; filename="'.$file.'"');
header('Content-Length: ' . filesize($file));
readfile($file);
 
?>
Open in New Window
Accepted Solution
 
02.01.2008 at 01:45AM PST, ID: 20795313
Thanks kebabs!

Anyone know how to do that in Perl?
 
02.01.2008 at 02:53AM PST, ID: 20795568
In addition to my last post, is there a way I could tell if the file download was completed or not?  Or better still, how much of it was completed (eg: 50% complete).

Thanks.
 
02.01.2008 at 08:36AM PST, ID: 20798047

Rank: Genius

I don't know of any way to tell if it was completed, other than maybe looking in the log file (maybe look at number of bytes transferred)..

Similar to the above, but in perl:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
#!/usr/bin/perl
use strict;
use CGI;
use CGI::Carp qw[fatalsToBrowser warningsToBrowser];
 
my $cgi = new CGI;
die "No file specified" unless $cgi->param('file');
 
#If you want to restrict to same dir, or something else...
die "Invalid file specified" if $cgi->param('file') =~ /\.\.\//;
 
print $cgi->header(
    'Content-Disposition' => "attachment; filename=$file",
    'Content-Length' => filesize($file),
    'type' => 'application/x-download');
open(IN,"<", $file) or die "Could not read file: $!\n";
print while(<IN>);
close(IN);
Open in New Window
 
02.01.2008 at 11:37PM PST, ID: 20803615
Looks great, Adam!  I plan to try your code soon.

When you talk of the number of bytes transferred (in a log file), are you talking about some file transfer info that the web host provides, or some other log file?
 
02.11.2008 at 04:20PM PST, ID: 20871622
Hi guys,

Having run Adam's script, I got these errors:
    Global symbol "$file" requires explicit package name at getfile.pl line 13.
    Global symbol "$file" requires explicit package name at getfile.pl line 14.
    Global symbol "$file" requires explicit package name at getfile.pl line 16.
    Execution of getfile.pl aborted due to compilation errors.

So, I changed 2 lines as follows:
    die "No file specified" unless my $file = $cgi->param('file');
    die "Invalid file specified" if $file =~ /\.\.\//;    # Optional change

But then I received this error:
    Undefined subroutine &main::filesize called at getfile.pl line 12.

To workaround this, I tried hard-coding the file size (3 bytes for this test):
        'Content-Length' => 3,
but got the same error, so I removed that line completely, and now I can download the file, but the file has the name "getfile.pl" instead of "MyFile.txt".

I know that $file contains "MyFile.txt", because I tried printing it after it was assigned.

How should I resolve all these issues (filesize and wrong filename)?

Thanks.
 
02.11.2008 at 10:20PM PST, ID: 20872815

Rank: Genius

Had a few bugs in that version of the code.  Try this version:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
#!/usr/bin/perl
use strict;
use CGI;
use CGI::Carp qw[fatalsToBrowser warningsToBrowser];
 
my $cgi = new CGI;
my $file = $cgi->param('file');
die "No file specified" unless $file;
 
#If you want to restrict to same dir, or something else...
die "Invalid file specified" if $file =~ /\.\.\//;
 
print $cgi->header(
    -Content_Disposition => "attachment; filename=$file",
    -Content_Length => -s $file,
    -type => 'application/x-download');
open(IN,"<", $file) or die "Could not read file: $!\n";
print while(<IN>);
close(IN);
Open in New Window
Assisted Solution
 
02.19.2008 at 01:04PM PST, ID: 20932559
Sorry for the delay in getting back to you guys.

Thanks Adam, that works perfectly!

Thanks kebabs for providing the original version (in PHP).

Thanks yo_s_canta for your JavaScript option.

Good to have you all on the EE team!
 
 
20080716-EE-VQP-33 / EE_QW_2_20070628