Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

redirect() vs location

by csorensen (Beadle)
on Jun 28, 2000 at 02:39 UTC ( [id://20097]=perlquestion: print w/replies, xml ) Need Help??

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

sometimes I have to include the header(text/html), line to make the redirect work .. and sometimes it won't work unless I remove that line I don't understand why this works the way it does I've read Lincoln's book and can't find a reason to include or not include header(text/html) it seems that sometimes it's implied .. and sometimes it's not am I missing something obvious ?? print header (text/html), print redirect($url$pick); but location always seems to work (and doesn't require cgi.pm) print "Location: $url$pick \n\n:;

Replies are listed 'Best First'.
Re: redirect() vs location
by btrott (Parson) on Jun 28, 2000 at 02:45 UTC
    You shouldn't have to call header before calling redirect. In fact, redirect shouldn't work properly after calling header, because header prints an entire HTTP response header.

    So it must be something else. Are you using the same URLs when you test redirect vs. Location? Be forewarned that using relative URLs often doesn't work properly. Could that explain the problem you're having?

Re: redirect() vs location
by chromatic (Archbishop) on Jun 28, 2000 at 08:12 UTC
    There are three types of headers a CGI script can send to the browser:
    • a Content-type header (text/html, for example)
    • a Location header (redirection URL)
    • a status code (the infamous 500, or a 404)
    Mixing and matching those will give you strange results, depending on the web browser.

    Solution? Just send the location header, if you want to use it. Send a status code header if you want the browser to display its specific error. Send the content-type header if you're returning data to be displayed.

    Update: I checked the relevant RFC for HTTP 1.1, and the status line header is sent before the Content-Type. So disregard that information, as BBQ is correct for HTTP 1.1. (Now I have to make a note about this book. Grr.)

      I was lead to beleive that after HTTP 1.1 all requests returned a status code, even if it is just a 200... I would suppose that Location would have a status code of its own, like temporarily redirected, or something of the sort. I can't remember where I heard this so please correct me if this is nonsense.

      #!/home/bbq/bin/perl
      # Trust no1!
        like temporarily redirected
        Yeah, that's 302. The specification is available at W3C's site.

        Slightly unrelated, but I read somewhere that the "Location: http://www.foo.com/" method is almost depracted.

        Rather, "URI: http://www.foo.com/" is the new-style approach that everyone should be using.

        And as far as I know, every HTTP request returns a status code.

Re: redirect() vs location
by cwest (Friar) on Jun 28, 2000 at 02:44 UTC
    I don't know what the problem is, use "Location: http://some.com\n\n".

    It always works and you don't have to load CGI.pm... what's the problem?

    The only thing I can think of is talked about in the CGI docs:

      One hint I can offer is that relative links may not work correctly when you
      generate a redirection to another document on your site.  This is due to a
      well-intentioned optimization that some servers use.  The solution to this
      is to use the full URL (including the http: part) of the document you are
      redirecting to.
    
    --
    Casey
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://20097]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-04-19 20:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found