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

I am trying to redirect to a html page in my script.

I am printing out the correct code:

print "Location: $url", "\n\n";

The only problem is, is that my script is printing out this line of code within the browser instead of actually redirecting me. I am not calling any print functions prior to this redirect and am not printing content type.

Any suggestions anyone.

Costas

Replies are listed 'Best First'.
Re: print and redirect
by voyager (Friar) on Apr 10, 2001 at 18:23 UTC
    I try not to mess with headers directly; let CGI.pm do the work:
    use CGI; my $url = 'www.somewhere.com'; print CGI::redirect($url);
      And to include the cookie functionality that costas later mentioned he wanted, you could do:

      use CGI; my $page = new CGI; my $url = 'www.somewhere.com'; my $cookie = $page->cookie(-name=>'sessionID', -value=>'xyzzy', -expires=>'+1h', -path=>'/cgi-bin/database', -domain=>'.foo.bar', -secure=>1); print $page->redirect(-location=>'http://somewhere.else/', -cookie=>$cookie);
Re: print and redirect
by Beatnik (Parson) on Apr 10, 2001 at 18:48 UTC
    The fact that your Location ends up in the browser is because you somehow already closed the HTTP header (the double \n closes it)... Make sure you close it when you intend to close it :))
    To answer your second question:

    Set-cookie: Cookie=Chocolate crisps\n"; Location: http://www.perlmonks.com\n\n"; # Closed now !
    Should work fine... Ofcourse you could use CGI.pm as mentioned above

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
      This works fine,

      However is it better coding practise for me to write the following?

      <html> <head> <meta http-equiv="refresh" content="0;url=http://redirect.url.here?any cgi=options&here=too"> </head> <body></body> </html>

      rather than just the meta refresh line

        In general I would say no, avoid http-equiv headers if you can modify the real http headers.

        However, it depends why you are redirecting, using http-equiv headers gives you the ability to include javascript etc. Using the 'real' http headers gives proxies a better chance of understanding what you're doing, and theoretically they could read the http header and prepare the redirect before the browser asked for it. I have no idea if this actually happens.

        I beleive that the w3 position is to avoid http-equiv headers.

        (Did I understand your question correctly??)
Re: print and redirect
by suaveant (Parson) on Apr 10, 2001 at 18:24 UTC
    You don't have it all...
    print "Status: 302 Moved Temporarily\n"; print "Location: $url\n\n";
    If it still shows in your page then you are probably printing out headers and a \n\n somewhere above
                    - Ant
      Ive realised where ive gone wrong, but its given me another problem.

      I am redirecting after i am creating a cookie. Since you can only write to a cookie once, does anyone know how i can write to a cookie and then immediately afterwards redirect to another page?

      Costas
        When I want to redirect a user I use the <meta> tag http-equiv=refresh - it's never given me any trouble. I would have your script write your cookie function and when it returns send the browser one of these:
        <meta http-equiv="refresh" content="0;url=http://redirect.url.here?anycgi=options&here=too">

        -Adam Stanley
        Nethosters, Inc.
        I don't think you can write a cookie in a redirect, I've tried before... you have to use a http-equiv refresh
                        - Ant