Reputation: 175
For an assignment I'm building a web application that starts with a user logging in. After logging in a session is created, but upon refreshing, new session IDs are being created, so state isn't being preserved. I've read about using the POST-REDIRECT-GET method to not resubmit the login form, and I'm trying to do this with my scripts. Maybe I'm misunderstanding how and what to redirect to, but this code below isn't working. It's a subroutine that is run once the user is authenticated. Any ideas of what's going wrong?
sub send_to_main {
my $session = new CGI::Session(undef, undef, {Directory=>'/tmp'});
$session->expires('+1d');
my $cookie = $q->cookie(
-name => 'jadrn015SID',
-value => $session->id,
-expires => '+1h');
print $q->header( -cookie=>$cookie );
my $sid = $session->id;
$session->param('user',$user); #added to make session more unique
print $cookie;
print $q->redirect(
-uri=>"http://jadran.sdsu.edu/perl/jadrn015/proj1_scripts/main_app.cgi",
-status=>302,
-cookie=>$cookie
);
EDIT: I am getting the 302 message back from the server, but it is being printed to my html document (I know this is because I am printing a header before I do the redirection). However, if I delete
print $q->header( -cookie=>$cookie );
Then nothing works, after logging in, my browser either says that the file is not found or it tries to download the script.
Upvotes: 0
Views: 1918
Reputation: 69264
As others have already pointed out, you are printing out too many things. All you should print is the redirection instruction.
print $q->redirect(
-uri=>"http://jadran.sdsu.edu/perl/jadrn015/proj1_scripts/main_app.cgi",
-status=>302,
-cookie=>$cookie
);
All other print statements need to be removed.
You say:
if I delete
print $q->header( -cookie=>$cookie );
Then nothing works, after logging in, my browser either says that the file is not found or it tries to download the script.
It seems to me that this is evidence that it is working. Looks like your browser is being redirected correctly and the errors that you are seeing are down to your second URL (the one that you redirect to) being either incorrect or misconfigured.
There are a couple of simple tests that you can run to demonstrate that it's working correctly.
Use curl
or something like that to make the HTTP request and see that you're getting the correct 302 response. Browsers are a terrible way to debug redirection problems as they go out of their way to hide what is actually going on.
Try visiting the redirection URL directly. I suspect you'll get the same problems, which will prove that the problems are down to the URL, not your redirection code.
Upvotes: 1
Reputation: 943563
print $q->header( -cookie=>$cookie );
The above takes the cookie and outputs it in an HTTP header
print $cookie;
The above takes the cookie, converts it to a string, then outputs it in the HTTP body.
print $q->redirect(
The above generates a redirect HTTP header … but you have already started the HTTP body … so it gets output as text instead.
Don't print the text of the cookie.
Upvotes: 1