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



Reply via email to