{"id":2857,"date":"2014-02-26T22:04:45","date_gmt":"2014-02-26T22:04:45","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/02\/26\/why-does-cgisession-new-and-load-fail-couldnt-thaw-collection-of-common-programming-errors\/"},"modified":"2014-02-26T22:04:45","modified_gmt":"2014-02-26T22:04:45","slug":"why-does-cgisession-new-and-load-fail-couldnt-thaw-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/02\/26\/why-does-cgisession-new-and-load-fail-couldnt-thaw-collection-of-common-programming-errors\/","title":{"rendered":"Why does CGI::Session new and load fail ( couldn&#39;t thaw() )?-Collection of common programming errors"},"content":{"rendered":"<p>I tried using the CGI::Session Library but for some reason my code won&#8217;t keep a persistant session &#8230; this is using Perl Moose for OOP, and is using Moose builders to instantiate the _cgi and _sss (session) parameters of a My::Session object&#8230;<\/p>\n<p><strong>UPDATED CODE<\/strong><\/p>\n<p><strong>My::Role::PersistantData<\/strong><\/p>\n<pre><code>package My::Role::PersistsData;\n\nuse Moose::Role;\nuse namespace::autoclean;\n  has '_cgi' =&gt; (\n           is        =&gt; 'rw', \n           isa       =&gt; 'Maybe[CGI]', \n           builder   =&gt; '_build_cgi'\n  );\n\n  has '_sss' =&gt; (\n           is        =&gt; 'rw', \n           isa       =&gt; 'Maybe[CGI::Session]',\n           builder   =&gt; '_build_sss'\n  );\n<\/code><\/pre>\n<p><strong>My::Session<\/strong><\/p>\n<pre><code>    package My::Session;\n\n    use Moose;\n    use namespace::autoclean;\n    with 'My::Role::PersistsData';  \n\n    use CGI;\n    use CGI::Session ('-ip_match');\n    use CGI::Carp qw\/fatalsToBrowser warningsToBrowser\/;\n\n    sub start{\n      my($self) = @_;\n      my $cgi = $self-&gt;cgi();\n      $self-&gt;log(\"Session Started!\");        \n    }\n\n\n    sub cgi{\n      my($self) = @_;\n      $self-&gt;_cgi = $self-&gt;_build_cgi() unless $self-&gt;_cgi;\n      return ($self-&gt;_cgi); \n    }\n\n\n    sub _build_cgi{\n      my($self) = @_; \n      my $cgi = CGI-&gt;new();\n\n      if(!$cgi){\n        #print \"mising cgi\";          \n      }\n\n      return ( $cgi );\n    }\n\n\n    sub _build_sss{\n          my($self) = @_;\n          my $cgi = $self-&gt;cgi(); \n\n          my $sid = $cgi-&gt;cookie(\"CGISESSID\") || $cgi-&gt;param('CGISESSID') || undef;\n\n          $self-&gt;log(\"Session ID Initial is: \".($sid?$sid:\"undef\"));\n\n          my $sss =  CGI::Session-&gt;new(undef, $cgi, {Directory=&gt;'tmp'})  or die CGI::Session-&gt;errstr;\n\n\n\n          my $cookie = $cgi-&gt;cookie(CGISESSID =&gt; $sss-&gt;id() );\n\n\n          $self-&gt;log(\"Resulting Session ID is: \".$sid.\" cookie is: \".$cookie);    \n\n          print $cgi-&gt;header( -cookie=&gt;$cookie );\n\n          return ( $sss );\n    }\n<\/code><\/pre>\n<p><strong>main.pl<\/strong><\/p>\n<pre><code>use Data::Dumper; \n$Data::Dumper::Sortkeys = 1;\n\nuse CGI;\nuse CGI::Carp qw(fatalsToBrowser);\nuse My::Session;\n\n$| = 1;\n$, = \" \";\n$\\ = \"\\n <br \/>\";\n\n  my $sss = My::Session-&gt;new();      \n  $sss-&gt;start();\n  print Dumper($sss);\n<\/code><\/pre>\n<p>It&#8217;s pretty weird because the first time I run this I get an actual CGISESSION ID and I am able to carry it over on a page refresh&#8230;<\/p>\n<p>however if I load the page again, suddenly the $sss (session) comes back as undefined, when it should return a new Session object:<\/p>\n<pre><code> $sss = new CGI::Session(\"driver:File\", $sid, {Directory=&gt;'\/tmp'})\n<\/code><\/pre>\n<p>for some reason $sss is coming back as undefined, which means it didnt initiate a new Session. A few tweaks to my code revealed this error:<\/p>\n<pre><code>new(): failed: load(): couldn't thaw() data using CGI::Session::Serialize::default:thaw(): couldn't thaw. syntax error at (eval 253) line 2, near \"\/&gt;\" \n<\/code><\/pre>\n<p>I also snooped around in CGI::Session.pm and found where this error was being thrown at, I guess it&#8217;s not able to parse _DATA or even read it&#8230;because of some strange characters&#8230; &#8220;\/&gt;&#8221;<\/p>\n<p><strong>CGI::Session.pm<\/strong><\/p>\n<pre><code>....\n    $self-&gt;{_DATA} = $self-&gt;{_OBJECTS}-&gt;{serializer}-&gt;thaw($raw_data);\n    unless ( defined $self-&gt;{_DATA} ) {\n        #die $raw_data . \"\\n\";\n        return $self-&gt;set_error( \"load(): couldn't thaw() data using $self-&gt;{_OBJECTS}-&gt;{serializer} :\" .\n                                $self-&gt;{_OBJECTS}-&gt;{serializer}-&gt;errstr );\n    }\n<\/code><\/pre>\n<p>Any idea why this isnt working?<\/p>\n<ol>\n<li>\n<p>For some reason you can&#8217;t use <code>Data::Dumper<\/code> or other HTML tags with <code>CGI::Session<\/code><\/p>\n<p>Answer found here and here<\/p>\n<p>Removing Dumper and HTML output fixed this problem &#8212; kind of &#8212;<\/p>\n<p><strong>updated<\/strong><\/p>\n<p>Apparently you have to use escapes<\/p>\n<pre><code> $cgi-&gt;escapeHTML ( Dumper($session) );\n<\/code><\/pre>\n<p>and that FINALLY resolves this problem.<\/p>\n<p>Perl is a pain!<\/p>\n<\/li>\n<li>\n<p>Most likely this is due to a different session cookie being sent (been there, hit that wall with head. HARD).<\/p>\n<p>Please print the session cookie value being used to store the session initially as well as session cookie value supplied by subsequent request.<\/p>\n<p>If they are indeed different, you have 2 options:<\/p>\n<ul>\n<li>\n<p>Investigate why different session cookie is sent by the browser in subsequent requests and fix that issue somehow.<\/p>\n<p>I was never able to find conclusive answer but my app consisted of a frame with internal so I suspect it was due to that.<\/p>\n<\/li>\n<li>\n<p>If like me you can&#8217;t find the root cause, you can also work around this.<\/p>\n<p>My workaround: explicitly STORING the original session cookie value as a form variable being passed around 100% of your code pieces.<\/p>\n<p>Then re-initialize session object with correct cookie value before your server side code requests session data.<\/p>\n<p>Not very secure, annoying, hard to get right. But works. I wouldn&#8217;t recommend it except as a uber-last-resort hack<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>Perhaps you could try (or at least look at the code to see how it works) for some stateful webapp module. I have used Continuity, very cool stuff.<\/p>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>I tried using the CGI::Session Library but for some reason my code won&#8217;t keep a persistant session &#8230; this is using Perl Moose for OOP, and is using Moose builders to instantiate the _cgi and _sss (session) parameters of a My::Session object&#8230; UPDATED CODE My::Role::PersistantData package My::Role::PersistsData; use Moose::Role; use namespace::autoclean; has &#8216;_cgi&#8217; =&gt; ( [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2857","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2857","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2857"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2857\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2857"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2857"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2857"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}