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

I realize HTML is stateless but I need a way to get the current page URL (with URL params, if any). I was told I could do it with JavaScript but they only way to get the data from JS would be to make a form with an invisible field.

Someone suggested that I use two ENV vars and piece them together manually but another said that it COULD work but you can't always rely on ENV variables as they can change performance from one server to the next.

So I come to you, oh Perl monks. How can I get my Perl script to know what URL it's loading?

Replies are listed 'Best First'.
Re: get page URL
by sgifford (Prior) on Apr 19, 2006 at 15:41 UTC
    Use CGI to do it:
    my $q = CGI->new(); my $url = $q->url(-absolute => 1);

    That should be as portable as CGI.pm, which is about as portable as you can get.

Re: get page URL
by kwaping (Priest) on Apr 19, 2006 at 14:29 UTC
    Well, Apache 1.3 at least has $ENV{'SCRIPT_URI'} and $ENV{'QUERY_STRING'}, which can easily be combined to give you the full URL with parameters. (This is probably in all Apache versions, but I am not familiar with anything else.) Does it need to be portable across all servers, or do you know exactly which server your script will be running on?

    ---
    It's all fine and dandy until someone has to look at the code.
      It has to be portable as this will be given out to many people on different web hosts/servers and all.
        Okay, that's good to know. Now another question - what are you going to use the URL for? Meaning, why does the script need to know it's own URL? Your answer might reveal another way to accomplish the same task.

        ---
        It's all fine and dandy until someone has to look at the code.