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

Alright, I'm trying to open another window, with the content being whatever url I supply it with. #!/usr/bin/perl
# NewPage.cgi
use CGI;
$q = new CGI;
print $q->header(-target=>'ResultWindow',-nhp=>1,-expires=>'now');
print $q->redirect(-uri=>'http://www.mindbase.net/',
-nph=>1);

Results are (in new window):
HTTP/1.0 302 Moved Status: 302 Moved Location: http://www.mindbase.net/

I know I'm missing something simple. This is just a test script. the finished product will be a html form passing a url to this script. Then this script opening the url in a new window.

Thanks ahead for any help,

Storm

Replies are listed 'Best First'.
Re: CGI and redirect
by jjhorner (Hermit) on May 24, 2000 at 17:14 UTC

    Try this:

    #!/usr/bin/perl # NewPage.cgi use CGI; $q = new CGI; print $q->header(-target=>'ResultWindow',-nhp=>1,-expires=>'now',-loca +tion=>'http://www.mindbase.net/');

    This will put the contents of the page in your browser with the correct address in the Address bar. I'm not sure if this is what you want, but it will do something similar.

    Update: If you want your address to stay in the browser Address bar, try this:

    #!/usr/bin/perl print "Location: http://www.mindbase.net/\n\n";

    J. J. Horner

    Linux, Perl, Apache, Stronghold, Unix

    jhorner@knoxlug.org http://www.knoxlug.org

      Perfect :) Thanks

      Storm
Re: CGI and redirect
by btrott (Parson) on May 24, 2000 at 19:22 UTC
    The reason this doesn't work is that you're issuing the redirect (which issues a Location header) *after* you've already completed the HTTP header. So to the browser, it just looks like it's getting the redirect as part of the content of the page.

    This should fix it:

    print $query->redirect(-target => 'ResultWindow', -expires => 'now +', -uri=>'http://www.mindbase.net/');
    What is nhp? Did you mean nph? It worked fine for me w/o that (as of course it would, since nhp doesn't seem to mean anything).
Re: CGI and redirect
by comatose (Monk) on May 24, 2000 at 17:31 UTC

    You probably want to use the Location header as jjhorner described. Redirect does not work like it appears that you believe it to.

    Redirect is for telling a browser that the site it was looking for has moved. Location is for redirects as most people think of them.

      the "Location:" HTTP header is the only way to do a redirect with HTTP (and there's also its crappy META HTTP-EQUIV cousin but that's HTML). Location and Redirect are one and the same thing.

      as btrott says below, the real problem was that the header function was terminating the HTTP headers (by means of a blank line), before the redirect function was called, so the "Location: ..." header was interpreted by your browser (correctly) as the page body.