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

I have a CGI script which is being called from many different pages. I need the CGI script to determine which page just called it. This was asked in the chatterbox earlier and it was said to be impossible; HTTP is stateless. But I won't accept impossible, somewhere, somehow someone has an idea of a workaround for this *crosses fingers*. Say we are at www.mypage.com/page1.shtml . We are including the CGI script as an SSI. The CGI has to output the full URL of page1.shtml. ( www.url.com/page.shtml )

Just for the sake of it, I also used

<!--#exec cgi="../cgi-bin/stats/downloads.pl"-->
My first idea was using $ENV{"HTTP_REFERER"}. I thought this worked at first but it turns out it only works if the page1.shtml is called directly. This is to say, if you are on www.mypage.com/page2.shtml and click a link to www.mypage.com/page1.shtml the CGI included on page1.shtml will say the referer was "www.mypage.com/page2.shtml" and NOT page1.shtml which it should!

I have used both the SSI and the exec cgi code and using HTTP_REFERER but it's absolutely not working. Can someone tell me how I can get the URL of the page that called it?

I could always pass param values of the URL to the script itself, that would work and I do realize that. The problem is this will be going on hundreds of pages and that's a lot of configuration time involved just to setup each param. If there's some HTTP echo command that can be passed to the CGI, that would probably work beautifully. I KNOW JS can do this, but I'm opposed to using JS all over the site.

Thank you wise monks, yet again.



"Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

sulfericacid

Replies are listed 'Best First'.
Re: Recalling the URL of initiating page
by skx (Parson) on Jan 24, 2005 at 10:15 UTC

    If you're working with a CGI you should get a whole bunch of information sent as environmental variables.

    One of them will be SCRIPT_NAME.

    Another useful one would be REQUEST_URI.

    See what your webserver (apache probably) will send to your script save the following as a CGI script and try calling it:

    #!/usr/bin/perl print "Content-type: text/html\n\n"; foreach my $key ( keys %ENV ) { print $key . " " . $ENV{$key} . "<br />\n"; }
    Steve
    ---
    steve.org.uk
Re: Recalling the URL of initiating page
by gellyfish (Monsignor) on Jan 24, 2005 at 10:56 UTC

    It is not 100% reliable but it seems that most servers will give the DOCUMENT_URI environment variable - we have used this in the NMS programs that are designed to be used with SSI and the only server we have had confirmed that this doesn't work for is Xitami.

    /J\

      Thank you!! $ENV{REQUEST_URI} and $ENV{DOCUMENT_URI} are just what I needed! It doesn't give the full URL but I can easily prepend my domain.

      This is perfect and it'll save hours of time instead of having to send params to the script.

      Thank you for your help everyone!



      "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

      sulfericacid
Re: Recalling the URL of initiating page
by PodMaster (Abbot) on Jan 24, 2005 at 08:42 UTC
    will say the referer was "www.mypage.com/page2.shtml" and NOT page1.shtml which it should!

    I have used both the SSI and the exec cgi code and using HTTP_REFERER but it's absolutely not working. Can someone tell me how I can get the URL of the page that called it?

    You should realize by now that this has everything to do with your webserver. So what you have to do is learn how your webserver works and figure out why HTTP_REFERER is not what you think it should be (this has nothing to do with perl).

    Have you examined the rest of %ENV?

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      I did actually do a dumb on %ENV before posting this message but none of them had a solution that would work. (this is the full list of %ENV that printed out).
      SCRIPT_NAME /cgi-bin/server-test.pl SERVER_NAME www.mypage.com SERVER_ADMIN webmaster@mypage.com HTTP_ACCEPT_ENCODING gzip, deflate HTTP_CONNECTION Keep-Alive REQUEST_METHOD GET HTTP_ACCEPT image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, appli +cation/x-shockwave-flash, */* SCRIPT_FILENAME /home/spyders/public_html/cgi-bin/server-test.pl SERVER_SOFTWARE QUERY_STRING REMOTE_PORT 60118 HTTP_USER_AGENT Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NE +T CLR 1.1.4322) SERVER_PORT 80 HTTP_ACCEPT_LANGUAGE en-us REMOTE_ADDR 66.47.159.11 SERVER_PROTOCOL HTTP/1.1 PATH /usr/local/bin:/usr/bin:/bin REQUEST_URI /cgi-bin/server-test.pl GATEWAY_INTERFACE CGI/1.1 SERVER_ADDR 216.32.90.218 DOCUMENT_ROOT /home/spyders/public_html HTTP_HOST www.mypage.com
      As other people mentioned below, $ENV{REQUEST_URI} works just fine. It's weird I didn't get that in my dump though.

      Thanks for your help.



      "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

      sulfericacid
Re: Recalling the URL of initiating page
by mkirank (Chaplain) on Jan 24, 2005 at 08:45 UTC
    Im not sure if this will work or not just a thought,
    If you are using Apache you could try to do URL Rewriting and pass the params ..
Re: Recalling the URL of initiating page
by TedPride (Priest) on Jan 24, 2005 at 12:46 UTC
    I use DOCUMENT_URI, just to second the above.
Re: Recalling the URL of initiating page
by r34d0nl1 (Pilgrim) on Jan 24, 2005 at 14:01 UTC
    I've been using $ENV{REQUEST_URI} under apache for a long time and it seems to work good.
    You could consider checking the configuration of your webserver or the correct set of your environment variables. :P