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

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.

Replies are listed 'Best First'.
Re^3: CGI error: "Invalid header value contains a newline not followed by whitespace"
by tobyink (Canon) on Feb 04, 2012 at 09:42 UTC

    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 say this because you've tried it? Or you say this because you trust the documentation?

        The documentation doesn't say that Apache ignores the cookie - it claims that browsers ignore cookies when they are redirected. It's wrong. They don't. (Though in some early versions of Safari there was a bug where it did. This was fixed though.)

        And if they did ignore cookies when redirecting, then why do you think that structuring your code slightly differently will change things? However your code works internally, if you're printing the same HTTP headers, restructuring the code won't change anything. Browsers don't see your code - they see the HTTP headers.

Re^3: CGI error: "Invalid header value contains a newline not followed by whitespace"
by Anonymous Monk on Feb 04, 2012 at 09:52 UTC

    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.

        But this doesn't actually redirect, which is a requirement for the particular login script where I first noticed the error on my machine.

        Sure it does