http://qs1969.pair.com?node_id=63782

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

I'm working on a new navigation system for my website, kinda like the one here (but not so smart). so all my links look something like : index.pl?location=SomePage&referer=MainPage now to control things, all outside links are specified like that (for logging): index.pl?location=Redirect&URL=http://www.somewhere.com/&referer=Links

The problem occured when I wanted to link to a page with a query like http://www.somewhere.com/page.cgi?id=something&type=html, so the link was like : index.pl?location=Redirect&URL=http://www.somewhere.com/page.cgi?id=something&type=html&referer=Links You get the idea

the URL query gets messed up, is there a way to get around this? or should I substitute the ampersands "&" with some other character, and then s/// them when redirecting?

any thoughts??


Chady | http://chady.net/

Replies are listed 'Best First'.
(dkubb) Re: (2) Oops! (Query String messed up)
by dkubb (Deacon) on Mar 12, 2001 at 14:20 UTC

    Well, you could use a regex to URL encode all the elements in your URL parameters, then join them together into a single query string.

    But, there is a much simpler way, using the CPAN module URI.

    Below is an example program using URI that does something similar to what you want:

    #!/usr/bin/perl -w use strict; use URI; use constant BASE_URL => 'index.pl'; use constant REFERER => 'http://search.cpan.org/search?dist=URI'; my $uri = URI->new(BASE_URL); $uri->query_form( location => 'Redirect', URL => REFERER, referer => 'Links', ); print $uri;

    A well formed URL will be printed out, and any parameters will be URL encoded properly.

    A nice bonus is that the code made with URI is easy to read and hides all the potentially messy URL construction/encoding details from you. I almost never work with URL's by hand any longer since I started to use this module.

Re: Oops! (Query String messed up)
by merlyn (Sage) on Mar 12, 2001 at 14:46 UTC