vit has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,
I need a simple solution, if possible, to create a kind of proxy in my Perl/CGI application for recording clicks on links.
This one
<a target=\"_blank\" href=$link><font size=\"1\" face=\"Verdana\" colo +r=\"gray\">$link_name</font></a>
opens a new site with URL = $link. I want to record URL every time the link is clicked. There should be some easy way.

Replies are listed 'Best First'.
Re: Control link clicks
by Corion (Patriarch) on Feb 11, 2010 at 14:37 UTC

    There is a very easy way. Just tell your users to write down the address whenever they click on a link, and have their sheets mailed to you monthly.

    You haven't told us your setup, so it's kinda hard to suggest you a solution.

    Potentially, you want to use just a simple redirector, except that such a redirector will be abused by people who want to anonymize their links:

    First you need to rewrite all your outbound links so they go through your redirector:

    <a href="/linkcounter?url=$link">Clicky clicky</a>

    You will need to URL-escape $link using URI::Escape but I'm too lazy to look that up now, and then you need to reconfigure your webserver to serve the counter+redirector under the URL /linkcounter:

    use strict; use CGI; my $cgi = CGI->new(); my $target = $cgi->param('url'); print "301 Elsewhere\r\n"; print "Location: $target\r\n"; print "\r\n";

    If all of this was about monitoring what links a local user clicks, the easy ways are to install a proxy server through which the user operates or to monitor what the user does, for example through WWW::Mechanize::Firefox.

Re: Control link clicks
by 7stud (Deacon) on Feb 11, 2010 at 15:22 UTC
    currently: your website another website \ /\ \ / \ html / \ / request \ / \/ / link clicker redirect: your website \ /\ ------------> another website \ \ request html \ \ \ \ \ \ request \/ \ link clicker javascript(can be turned off): your website another website ^ \ /\ ` \ / ` \html / request js ` \ / ` \ / ` \/ / link clicker
      Do you mean I need to use CGI redirect method like here?
      use CGI; my $query=new CGI; print $query->redirect('http://www.foo.com');