in reply to CGI error: "Invalid header value contains a newline not followed by whitespace"

I think that your problem is:
print $q->header( -cookie => [ $set_session, $set_username ] , $q->re +direct("http://localhost/"));
This works for me:
#!/usr/bin/perl use strict; use warnings; use CGI; my $user_name = 'user'; my $secret = 'password'; my $q = CGI->new; my $set_username = $q->cookie( -name => "user_name", -value => $user_name, -expires => "+1d", ); my $set_session = $q->cookie( -name => "session", -value => $secret, -expires => "+1d", ); print $q->header( -cookie => [ $set_session, $set_username ]); print $q->redirect('http://localhost/');

Replies are listed 'Best First'.
Re^2: CGI error: "Invalid header value contains a newline not followed by whitespace"
by MyMonkName (Acolyte) on Feb 04, 2012 at 08:55 UTC

    On my box, that code also fails, differently. It sends the cookies, but then prints the HTTP 302 code to the browser, rather than redirecting, like so:

    Status: 302 Found Location: http://localhost

    However, monkeying around with your code, I made this slight adjustment (look closely to see the difference!):

    print $q->header( -cookie => [ $set_session, $set_username ], print $q->redirect('http://localhost/'));

    ... and lo! my script works again! :D

    That's right, on my box, I will now have to insert a superfluous print in all of my cgi scripts that happen to send a redirect in the html header. How bizarre. But thanks to the both of you for the help!

    FYI, I experienced this behaviour with both whatever version of CGI.pm came with Lucid (didn't check), and then with the latest from git, 3.59 I guess.

      The correct code is:

      print $q->redirect( -uri => 'http://localhost/', -cookie => [ $set_session, $set_username ], );
        Not really. Per the documentation, apache will ignore cookie headers in a redirect request.

      You only print headers once, either use redirect, or use header, not both

      print $q->header( -status => '302 Found', -uri => 'http://localhost/', -cookie => [ $set_session $set_username ], ); __END__
        But this doesn't actually redirect, which is a requirement for the particular login script where I first noticed the error on my machine.