You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Sumitro Chowdhury <su...@yahoo.com> on 2002/11/05 01:52:27 UTC

redirection halts if a message is printed

Hello,
I see redirection behaves differently when a message
is printed beforehand then when redirection happens
alone.

Case I ( simple redirection ):
------------------------------
package redirect.pm

use Apache::Const -compile =>
qw(HTTP_MOVED_TEMPORARILY);

sub handler (
   my $r = shift;
   $r->content_type('text/html');
   $r->headers_out->{'Location'}=
"http://new.location";
   return HTTP_MOVED_TEMPORARILY;
}
1;


Result is : browser immediately loads the new page.

Case II ( Print a message before redirection )
----------------------------------------------
package redirect.pm

use Apache::Const -compile =>
qw(HTTP_MOVED_TEMPORARILY);

sub handler (
   my $r = shift;
   $r->content_type('text/html');
   print "Redirecting to new site ...";
   $r->headers_out->{'Location'}=
"http://new.location";
   return HTTP_MOVED_TEMPORARILY;
}
1;


Result is : Instead of opening the new URL, the
following is displayed on the browser :
Redirecting to new site ...
Ok
The document has moved here (here is clickable).
...

This is the output of "view page source":

Redirecting to new site ...<!DOCTYPE HTML PUBLIC
"-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>200 OK</title>
</head><body>
<h1>OK</h1>
<p>The document has moved <a
href="http://new.location">here</a>.</p>
<hr />
<address>Apache/2.0.43 Server at myserver.company.com
Port 80</address>
</body></html>


My question is : 
a)What is the reason for this behaviour ? Why does
printing a message halt the redirection ?
b)What needs to be done to display a message saying
"Redirecting .." and then the browser to auto-display
the new URL ?

Thanks in advance ..
Sumitro Chowdhury.


__________________________________________________
Do you Yahoo!?
HotJobs - Search new jobs daily now
http://hotjobs.yahoo.com/

Re: redirection halts if a message is printed

Posted by Sumitro Chowdhury <su...@yahoo.com>.
Understood. 
Thanks to Chris and Richard. 
Sumitro Chowdhury. 
 



---------------------------------
Do you Yahoo!?
HotJobs - Search new jobs daily now

Re: redirection halts if a message is printed

Posted by Chris Shiflett <ch...@shiflett.org>.
Sumitro Chowdhury wrote:

>Case I ( simple redirection ):
>------------------------------
>package redirect.pm
>
>use Apache::Const -compile =>
>qw(HTTP_MOVED_TEMPORARILY);
>
>sub handler (
>   my $r = shift;
>   $r->content_type('text/html');
>   $r->headers_out->{'Location'}=
>"http://new.location";
>   return HTTP_MOVED_TEMPORARILY;
>}
>1;
>
>Result is : browser immediately loads the new page.
>
>Case II ( Print a message before redirection )
>----------------------------------------------
>package redirect.pm
>
>use Apache::Const -compile =>
>qw(HTTP_MOVED_TEMPORARILY);
>
>sub handler (
>   my $r = shift;
>   $r->content_type('text/html');
>   print "Redirecting to new site ...";
>   $r->headers_out->{'Location'}=
>"http://new.location";
>   return HTTP_MOVED_TEMPORARILY;
>}
>1;
>
>Result is : Instead of opening the new URL, the
>following is displayed on the browser :
>Redirecting to new site ...
>Ok
>The document has moved here (here is clickable).
>...
>
>My question is : 
>a)What is the reason for this behaviour ? Why does
>printing a message halt the redirection ?
>b)What needs to be done to display a message saying
>"Redirecting .." and then the browser to auto-display
>the new URL ?
>

a. When your script outputs something (sending a message as you say), 
you are telling Apache that you are finished sending the HTTP headers 
and have begun the content part of the response. Thus, it is too late 
for the Location header you later want to send. This is a protocol 
restriction based on the construction of an HTTP message. To avoid this, 
you have to either send all of your HTTP headers first (my preference), 
or you need to buffer your output until you are finished sending your 
headers.

b. It sounds like you are wanting the user to visit an intermediate page 
displaying a "Redirecting ..." message before being forwarded to a final 
destination. In this case, you probably want to use HTTP's Refresh 
header or the HTML equivalent: <meta http-equiv="refresh" value="???">. 
Both have the same syntax:

3; url=http://example.org/

In this example, the 3 is the number of seconds you want the client to 
wait prior to sending the next request, which will be for the URL 
http://example.org/.

Chris


Re: redirection halts if a message is printed

Posted by Richard Clarke <ri...@likewhoa.com>.
Sumitro Chowdhury wrote:

><snip>
>
>
>My question is : 
>a)What is the reason for this behaviour ? Why does
>printing a message halt the redirection ?
>
I don't know how to explain this really but, HTTP_MOVED_TEMPORARILY 
implies a 302 status code which means,

    "The requested resource resides temporarily under a different URI."

Print some text to the browser implies a 200 OK code meaning,

    "The request has succeeded."

These are both conflicting status'. The httpd can't send 2 status codes 
and as far as I am aware there is no scope in the HTTP protocol for 
returning a message in the response body about the fact that we are 
"Redirecting to a new site....".

>b)What needs to be done to display a message saying
>"Redirecting .." and then the browser to auto-display
>the new URL ?
>
http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q=meta+refresh&btnG=Google+Search
    http://www.pageresource.com/html/metref.htm
    http://www.netmechanic.com/news/vol4/promo_no15.htm
    etc..

Any one of these should help you solve your problem I think

>Thanks in advance ..
>Sumitro Chowdhury.
>
>  
>
Hope this helps,
    Richard Clarke.