groengoen has asked for the wisdom of the Perl Monks concerning the following question:

This is the same title as a previous question but the error occurs in different circumstances. I am a complete newby with CGI and PERL and was just looking to debug a CGI script.

###################################################################### +############################################### # LOGOUT ###################################################################### +############################################### sub logout { my ($cgi) = @_; my $cookie1 = $cgi->cookie(-name => "login", -value => "", -expires => '-1d'); my $cookie2 = $cgi->cookie(-name => 'password', -value => "", -expires => '-1d'); my $cookie3 = $cgi->cookie(-name => 'id', -value => "", -expires => '-1d'); my $cookie4 = $cgi->cookie(-name => 'CGISESSID', -value => "", -expires => '-1d'); my $cookie = $cookie1."\nSet-Cookie: $cookie2"."\nSet-Cookie: $cooki +e3\nSet-Cookie: $cookie4"; print $cgi->redirect(-cookie => $cookie, -uri => "$PROTOCOL://$ENV{HTTP_HOST}$ENV{SCR +IPT_NAME}" ); return; }
This is to log out from the script and redirect to the previous screen.
  • Comment on CGI error: "Invalid header value contains a newline not followed by whitespace"
  • Download Code

Replies are listed 'Best First'.
Re: CGI error: "Invalid header value contains a newline not followed by whitespace"
by Corion (Patriarch) on Oct 13, 2014 at 20:16 UTC

    You build a header value that contains newlines, and these newlines are not immediately followed by whitespace. This is what CGI.pm complains about, as doing so is usually an error.

    Your code seems to try to abuse that function to set multiple cookies. Maybe you want to ask how to set multiple cookies using CGI.pm instead? Unsurprisingly, searching CGI for "multiple cookies" gives a ready result. Maybe you want to try that?

Re: CGI error: "Invalid header value contains a newline not followed by whitespace"
by GrandFather (Saint) on Oct 13, 2014 at 20:15 UTC
Re: CGI error: "Invalid header value contains a newline not followed by whitespace"
by toolic (Bishop) on Oct 13, 2014 at 20:16 UTC
Re: CGI error: "Invalid header value contains a newline not followed by whitespace"
by ikegami (Patriarch) on Oct 14, 2014 at 06:49 UTC
    $cookie does not contain a cookie or a reference to an array of cookies. It's garbage.
    my $cookie = $cookie1."\nSet-Cookie: $cookie2"."\nSet-Cookie: $cookie3 +\nSet-Cookie: $cookie4";
    should be
    my $cookie = [ $cookie1, $cookie2, $cookie3, $cookie4 ];

    Then we can clean up your code.

    my @cookies; push @cookies, $cgi->cookie(-name => "login", -value => "", -expir +es => '-1d'); push @cookies, $cgi->cookie(-name => 'password', -value => "", -expir +es => '-1d'); push @cookies, $cgi->cookie(-name => 'id', -value => "", -expir +es => '-1d'); push @cookies, $cgi->cookie(-name => 'CGISESSID', -value => "", -expir +es => '-1d'); print $cgi->redirect( -cookie => \@cookies, -uri => $cgi->url(), );
    or even
    my @cookies = map { $cgi->cookie(-name => $_ -value => "", -expires => + '-1d') } qw( login password id CGISESSID ); print $cgi->redirect( -cookie => \@cookies, -uri => $cgi->url(), );
      Thanks very much for the very concise code. I used the last bit of code and it eliminated the error. However I actually wanted the logout to redirect to the calling url for the whole cgi. I am actually returned to the page with the logout tab(where I logged out). Any idea about that?

        Because you're passing the url of the page the user requested when you mean to redirect to the login page.

        Maybe $cgi->url(-base => 1) . '/login.html'?