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

I have 2 cgi scripts clickcount and countlink they where both about the same, They count how many times a link or links have been clicked on. (simple right) The scripts run fine as if I was a normal person (guess I'm not) I have to track links with spaces in them, (the dilemma begins) as any one would have guessed it the scripts don't work when you redirect to a URL with spaces in it. You end up with some %20 or ?20 where the spaces are supposed to be. So the question is how do I parse the links to redirect correctly. (I don't want the script to put the %20 or ?20) Oh to make it a little more difficult and then again maybe there is a simple solution. I cant change the links.

example
good - www.whatever.com/all mikes are cool/file.zip
bad - www.whatever.com/all%20mikes%20are%20cool/file.zip

I really hope someone can solve this problem
thanks
mike

  • Comment on problem endcoding URLs with embedded spaces

Replies are listed 'Best First'.
Re: problem link tracking
by Chrisf (Friar) on Jan 15, 2002 at 21:28 UTC
    If you use the CGI.pm module it will do all the work for you.
    use strict; use CGI; my $q = new CGI; my $url = $q->param("url"); # Log data print $q->redirect("$url");
Re: problem link tracking
by screamingeagle (Curate) on Jan 15, 2002 at 23:17 UTC
    try using this :
    $url =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;