tags:

views:

304

answers:

4

Hello stackoverflow,

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"} ) {
    print header(-refresh=>'0; homepage.pl.cgi');
}

What fundamentals am I not understanding here?

Thanks, CB

A: 

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.

Jason
No,correcting the capitalization did not affect this.
cb
That might be it now. It's been a long time since I've manually written a refresh, so I looked up the syntax to make sure it was right.
Jason
no luck with that change either. Would it matter that I've recently print header(-cookie=>$cookie) in the preceding code block?
cb
That's probably it. The header() function from CGI.pm ends it with a blank line, which signifies the end of the header to the browser. There may be a way to buffer it so it doesn't send them until you're ready, but it's been awhile and I can't recall for sure.
Jason
That is definitely it, you should only print the header once.
Kinopiko
Could you suggest a better way for my logic flow then? My idea now is validate password/username in startPage.pl.cgi->set sessionID cookie->redirect into the homepage.pl.cgi suggestions anyone?
cb
You might try storing your headers in a variable until you're sure you are ready to send them.
Jason
A: 

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.

Myles
Okay, I attempted to replace the offending line with print redirect('homepage.pl.cgi');but no luck.Now I just get:Status: 302 Moved Location: homepage.pl.cgi
cb
A 307/Temporary redirect is probably more appropriate, but it still requires you to solve the duplicate header problem.
Jason
I disagree with the 307 due to the fact that it doesn't appear to be temporary, but rather an expected redirection.
Myles
A: 

Do you have an exit after that?

If you're refreshing your original script should not produce further output. If it does this might explain the problem.

Steve Kemp
+1  A: 

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.
# ...
Ether
Thanks, as far as I can tell that did the trick and I learned a valuable lesson: only one header per page! I appreciate everybody's thoughtful answers to my problem. This project isn't complete yet so CGI experts, be ready for more! -cb
cb