开发者

Why doesn't my Refresh header work?

I'm working on a homework assignment in Perl CGI using the CGI.pm module. In my code I am checking for a cookie. If the cookie exists, I want to initiate another CGI script. In other situations I was able to use similar code, but in this instance I merely get the following browser output, not the redirect that I was looking for.

Refresh: 1; URL=homepage.pl.cgi
Content-Type: text/html; charset=ISO-8859-1

Here's my code:

#get the cookie
my %SIDhash = cookie('SIDhash');

if ( exists $SIDhash{"SID"} ) {
    p开发者_JAVA技巧rint header(-refresh=>'0; homepage.pl.cgi');
}

What fundamentals am I not understanding here?

Thanks, CB


This should do the trick:

print header(
    -refresh => '0; url=homepage.pl.cgi',
    -cookie => $cookie,
);

If you are assembling the header in pieces, in various places in your code, save the header components in a variable first, e.g.:

my %headers;

# later...
$headers{-cookie} = $cookie;

# later still:
if (exists $SIDhash{SID})
{
    # we want to redirect, so print all headers and we're done.
    print header(%headers, -refresh => '0; url=homepage.pl.cgi');
    exit;
}

# if we're still here, nothing is printed yet.. continue preparing data and print when ready.
# ...


I'm not sure why your refresh doesn't work, but it sounds like it would be more appropriate to use:

HTTP/1.1 302 Found
Location: http://www.example.org/

Just a thought.


Try changing the line to

print header(-refresh=>'0; url=homepage.pl.cgi');

From what I can tell, this should be correct now.

This page on Wikipedia offers information on Refresh and other methods of redirection.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜