Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Rebuilding GET Parameters without ENV{'REQUEST_URI'}

by svsingh (Priest)
on Jul 01, 2004 at 17:36 UTC ( #371184=perlquestion: print w/replies, xml ) Need Help??

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

I wrote a CGI script that links back to itself (including GET parameters). I created the link with the following statement:
my $url = "http://$ENV{'SERVER_NAME'}/$ENV{'REQUEST_URI'}";

When I moved my script to the production server, I discovered that REQUEST_URI is not in the environment. My test environment uses Apache and the production environment uses IIS.

Without upgrading the production server to Apache, is there a clean way to get the parameter list? I'm looking at the contents of %ENV, but nothing seems to have the parameters.

My current plan is to use $ENV{'PATH_INFO'} and loop through the parameters to create the URL (only if URI_INFO doesn't exists). The code would look something like:

my $url = "http://$ENV{'SERVER_NAME'}/$ENV{'PATH_INFO'}?"; foreach (param) { $url .= "&$_=" . param($_); }

Is there an easier or better way to do this? I've searched PM and the docs, but nothing's jumping out at me. Thanks.

Replies are listed 'Best First'.
Re: Rebuilding GET Parameters without ENV{'REQUEST_URI'}
by matija (Priest) on Jul 01, 2004 at 17:50 UTC
    The generic way to do stuff like this is to use CGI. That module contains everything you need to read and parse CGI parameters. It also contains the function which returns the details of the server and the URL of the script.

    Look for url and self_url in the CGI docs.

Re: Rebuilding GET Parameters without ENV{'REQUEST_URI'}
by cees (Curate) on Jul 01, 2004 at 17:55 UTC

    If you are able to use the module, it has a method called self_url which will give you the results that you are after (not having used IIS, I can't be possitive these methods will work under IIS).

    Search through the docs for "CREATING A SELF-REFERENCING URL THAT PRESERVES STATE INFORMATION" for more information of how to build URLs based on the current request.

    - Cees

Re: Rebuilding GET Parameters without ENV{'REQUEST_URI'}
by svsingh (Priest) on Jul 01, 2004 at 18:07 UTC
    Thank you both! That worked perfectly. Here's the final line that works for both Apache and IIS servers:
    my $url = self_url; # get the url from CGI

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://371184]
Approved by Old_Gray_Bear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (9)
As of 2021-04-14 17:05 GMT
Find Nodes?
    Voting Booth?

    No recent polls found