http://www.perlmonks.org?node_id=36008

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

How can I pass the URL of the page I am on?

Originally posted as a Categorized Question.

  • Comment on How can I pass the URL of the page I am on?

Replies are listed 'Best First'.
Re: How can I pass the URL of the page I am on?
by AgentM (Curate) on Oct 10, 2000 at 02:22 UTC
    Assuming you wish to pass the URL from a CGI script to the next page:
    use CGI; ... $query->hidden('URL',$query->self_url); ...
    Assuming you wish to pass the URL from the web page to the CGI.
    1. on your page create a hidden field that contains the pertinent information
    2. use the param() function in CGI to retrieve the variable and voila!
    In the future, please be more specific in your question and post them to Seekers of Perl Wisdom instead.
Re: How can I pass the URL of the page I am on?
by cianoz (Friar) on Oct 10, 2000 at 21:25 UTC
    ...and if you can't use forms then:
    use URI::Escape; my $url = $query->self_url; $url = uri_escape($url); # or $query->url; it depends.. print qq{<a href="/path/to/new/location.pl?URL=$url">new location</a>} +;
      The result of self_url is already URI-escaped, but if you're feeding it as HTML, you need to additionally HTML-entitize it.
      use HTML::Entities; my $url = encode_entities(CGI->self_url()); print qq{<a href="/path/to/new/location.pl?URL=$url">new location</a>} +;

      -- Randal L. Schwartz, Perl hacker

Re: How can I pass the URL of the page I am on?
by Trimbach (Curate) on Oct 10, 2000 at 23:45 UTC
    ... and if you want to find out the URL of the webpage that triggered your CGI in the first all you have to do is:
    $referrer=$ENV{'HTTP_REFERER'};
    and that's that. No CGI.pm, no forms, no nothin'.
      And not reliable!
      • Referer may be missing (stripped by security software and/or firewalls)
      • Referer may be wrong (some browsers blow it on the spec)
      • Referer is trivially faked
      The big clue is, how can you rely on a variable with an incorrectly spelled name!

      -- Randal L. Schwartz, Perl hacker

Re: How can I pass the URL of the page I am on?
by mendeepak (Scribe) on Mar 08, 2012 at 12:08 UTC

    if you want current browser url you can use print $ENV{'HTTP_REFERER'} and if you want to get the current files server path use

    print $0

      "if you want current browser url you can use print $ENV{'HTTP_REFERER'}"

      This isn't correct. HTTP Referer tells the URL of the previous page, not the current URL. Further reading: HTTP_referer.