in reply to Need help with redirect

here it just prints "Status: 302 ...

This is because you've already output

print "Content-type: text/html\n\n";

before issuing the redirect.  (It must be the first thing output by the script, otherwise - if you print it after the header section - it will be treated as regular content.)

Replies are listed 'Best First'.
Re^2: Need help with redirect
by MatthewV (Acolyte) on Jun 02, 2009 at 01:16 UTC
    That makes sense. I moved the print content type line and now I get a big "Object Moved This Document can be found here" message, still no redirect.

      I think what you're trying to do (using a HTTP redirection header with SSI) isn't going to work that way. I don't usually use SSI, but I think the additional magic in the handling of content vs. headers that would be required here simply isn't implemented with most web servers.

      Essentially, there are three ways to redirect:

      • HTTP redirect (what you're trying)
      • HTML <meta http-equiv="refresh" content="0;url=http://..." />
      • JavaScript

      You could try method 2, though, if you really need to embed the script's output into some HTML page via SSI.  Using <meta http-equiv="refresh" ...>, the redirect instruction becomes part of the HTML page, which you've already begun outputting, when the SSI instruction is being encountered.

        To use method two, he'd have to move the SSI below the TITLE element.

        <!DOCTYPE html ...> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>...</title> <!--#exec cgi="/ssi/checkcookie.pl"--> ... </head> ... </html>
        a meta refresh would not work. I am using the SSI to protect certain pages. This is the first time I have tried to password protect a member's section of my site. Using a meta refresh can be easly bypassedby hitting the escape key fast enough. Using the SSI as the frisst thing called should allow the redirect to happen before the protected content is loaded.

      Update: I didn't clue in that you were launching the script via SSI. In SSI includes, the header is apparently ignored (which makes sense cause you can't send multiple headers). See almut's reply instead.


      It works fine for me

      #!/usr/bin/perl -w use strict; use CGI q~:standard~; use DBI; use CGI::Cookie; use CGI::Carp q~fatalsToBrowser~; print redirect(-location => 'http://admin.ffinfo.com/');

      But I could see it failing if the script was fetched executed using POST instead of GET.

      If the 302 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.

      You may want a 303.

      The response to the request can be found under a different URI and SHOULD be retrieved using a GET method on that resource. This method exists primarily to allow the output of a POST-activated script to redirect the user agent to a selected resource.

      If you can't get it to work, execute the script from the prompt and show us what you get. You may also want to find out what the browser is receiving using a browser plugin or a packet sniffer.

        Right the code works when called by it's self. It is when I call it the script via SSI that it does not work. I added the -Status => 303 command and get the same error.
      That is called a redirect -- you can configure browsers to ignore redirects