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

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

Mindful Monks,

I have a standard HTML form for search engine input on a regular HTML page. The form action is set to use GET and to call not a Perl script, but an SHTML page with an embedded SSI-call to a Perl script, like this:

Search Page

<FORM method="GET" action="search_results.shtml"> <input type="text" name="query"> <input type="submit"> </form>

search_results.shtml

<html> <head></head> <body> <!--#include virtual="/includes/header.htm" --> <!--#include virtual="/cgi-bin/search_script.pl" --> <!--#include virtual="/includes/footer.htm" --> </html>

The idea was that the full url of search_results.shtml, being called from a form using GET, will have the search terms tacked on the end as a query substring. So, we'd have search_script.pl read $ENV{HTTP_REFERER}, which is search_Results.pl, and strip the substring off to get the origial search terms.

It would be nice to have cgi-pm do this stripping and formatting, but I don't konw how to get it to do that.

Of course, any smarter way of doing this is welcome too.

Thanks.





Forget that fear of gravity,
Get a little savagery in your life.

Replies are listed 'Best First'.
Re: Extract form fields & values from query string of http_referrer
by Leviathan (Scribe) on Oct 18, 2006 at 07:26 UTC

    You can initialize CGI.pm with the query you want, like this:

    my $query = (split /\?/, $ENV{HTTP_REFERER}, 2)[1]; my $q = new CGI($query); ...

    But you cannot always rely on HTTP_REFERER to be set, so why not post the query directly the the perl script?

    --
    Leviathan.
Re: Extract form fields & values from query string of http_referrer
by shmem (Chancellor) on Oct 18, 2006 at 07:29 UTC
    Rewrite your %ENV before initializing the CGI object:
    use CGI; use URI; $ENV{'QUERY_STRING'} = URI->new($ENV{'HTTP_REFERER'})->query; my $q = CGI->new; ...

    Now you can access the referer's parameters with $q->param(whatever).

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Extract form fields & values from query string of http_referrer
by monarch (Priest) on Oct 18, 2006 at 07:27 UTC
    I'm unsure exactly what you're trying to achieve. Are you expecting a URL like search_results.shtml?query=this+is+my+query and you want to pass the "query=this+is+my+query" portion to search_script.pl?

    I've used the following SSI code (the Apache 2.0 mod_include documentation is a good read) for extracting cookies and turning them into query-strings which may or may not help you:

    <!--#if expr='$HTTP_COOKIE = /query=([^;]+)/' --> <!--#set var='queryval' value='$1' --> <!--#elif expr='$QUERY_STRING = /query=([^\x26]+)/' --> <!--#set var='queryval' value='$1' --> <!--#else --> <!--#set var='queryval' value='' --> <!--#endif --> <!--#include virtual="/cgi-bin/search_script.pl?query=$queryval" -->

    Some notes on the above. This code hunts for a cookie named "query". If it exists it sets an environment variable called "queryval" with the contents of that cookie. Otherwise it looks for the "query" parameter in the query string (passed by the HTTP GET). If it finds it then the environment variable "queryval" gets set. Otherwise "queryval" is set to nothing.

    Oh, and \x26 is an ampersand (&). I escaped this character for my own personal reasons (you can just replace it with & if you prefer).

    I know what I've provided is a bit more convoluted but is this along the lines of what you're trying to find out?

Re: Extract form fields & values from query string of http_referrer
by punch_card_don (Curate) on Oct 24, 2006 at 00:49 UTC
    Thanks all.

    Working from Leviathan's idea, I ended up doing:

    $query = new CGI($ENV{'REQUEST_URI'});
    and that works just fine.




    Forget that fear of gravity,
    Get a little savagery in your life.
      I need to do something similiar and I am stumped as to how to accomplish it. Here's the scenario.. users are in my website, they click on something say reservation for example then they click on ? to get help, my ? redirects them to a support website which contains site specific information and resources, one of those resources is the helpfile, I need to map into the helpfile depending on what is returned into the help system via the query string, specifically I need to set iframe src= to that portion of the helpfile. I thought it would be easisest to do this using a hash, so I setup a hash with the key topic and the value=to the url I want to insert as content into the helpframe, with the src equal to that location the target should be the iframe for each src, I cannot for the life of me figure out how to pass the info into my file. my file is asp..and I have my hash but I cannot figure how to match my topic with the topic keys and then return the value, then aftewards set the iframe src to it.. its baffling the almighty h out of me. can you help?