Quick fix for “Expected token not present” in Apache2::Cookie

by Thomas Beutel

A client’s website of mine had been running fine for many years until a few weeks ago. Certain users of IE started getting this error: “Expected token not present” and were no longer able to login. My guess is that the design team switched some elements from Flash to HTML5/jQuery and introduced some bad cookie code in Javascript. Not wanting to dive into a bunch of JS or jQuery code, I looked into solving the problem on the server side.

I tracked it down to Apache2::Cookie rejecting cookies with invalid formatting and then exiting. So I took the advice on this page and made a change the fetch method of Cookie.pm:

#my $jar = $req->jar or return;
#...avoid cookie parse error "Expected token not present"
my $jar = eval {$req->jar()} || $@->jar; # if fail, parse the cookie anyway
return unless $jar;

I don’t like making direct changes to installed modules like this, but in this case I made an exception. The website in question will be put on a PHP platform soon and it wasn’t worth spending more time on this problem.