为什么 CGI::Session new 和 load 失败(无法 thaw() )?

发布于 2024-12-05 03:20:00 字数 3295 浏览 1 评论 0原文

我尝试使用 CGI::Session 库,但由于某种原因,我的代码不会保留持久会话...这是使用 Perl Moose 进行 OOP,并使用 Moose 构建器来实例化 _cgi 和 _sss (会话)参数My::Session 对象...

更新代码

My::Role::PersistantData

package My::Role::PersistsData;

use Moose::Role;
use namespace::autoclean;
  has '_cgi' => (
           is        => 'rw', 
           isa       => 'Maybe[CGI]', 
           builder   => '_build_cgi'
  );

  has '_sss' => (
           is        => 'rw', 
           isa       => 'Maybe[CGI::Session]',
           builder   => '_build_sss'
  );

My::Session

    package My::Session;

    use Moose;
    use namespace::autoclean;
    with 'My::Role::PersistsData';  

    use CGI;
    use CGI::Session ('-ip_match');
    use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;

    sub start{
      my($self) = @_;
      my $cgi = $self->cgi();
      $self->log("Session Started!");        
    }


    sub cgi{
      my($self) = @_;
      $self->_cgi = $self->_build_cgi() unless $self->_cgi;
      return ($self->_cgi); 
    }


    sub _build_cgi{
      my($self) = @_; 
      my $cgi = CGI->new();

      if(!$cgi){
        #print "mising cgi";          
      }

      return ( $cgi );
    }


    sub _build_sss{
          my($self) = @_;
          my $cgi = $self->cgi(); 

          my $sid = $cgi->cookie("CGISESSID") || $cgi->param('CGISESSID') || undef;

          $self->log("Session ID Initial is: ".($sid?$sid:"undef"));

          my $sss =  CGI::Session->new(undef, $cgi, {Directory=>'tmp'})  or die CGI::Session->errstr;



          my $cookie = $cgi->cookie(CGISESSID => $sss->id() );


          $self->log("Resulting Session ID is: ".$sid." cookie is: ".$cookie);    

          print $cgi->header( -cookie=>$cookie );

          return ( $sss );
    }

ma​​in.pl

use Data::Dumper; 
$Data::Dumper::Sortkeys = 1;

use CGI;
use CGI::Carp qw(fatalsToBrowser);
use My::Session;

$| = 1;
$, = " ";
$\ = "\n <br />";

  my $sss = My::Session->new();      
  $sss->start();
  print Dumper($sss);

这很奇怪,因为我第一次运行这个时,我得到了一个实际的 CGISESSION ID,并且我能够在页面刷新时携带它......

但是,如果我再次加载页面,突然当 $sss (会话)应该返回一个新的 Session 对象时,它会以未定义的形式返回:

 $sss = new CGI::Session("driver:File", $sid, {Directory=>'/tmp'})

由于某种原因,$sss 以未定义的形式返回,这意味着它没有启动新的会话。对我的代码进行了一些调整,发现了这个错误:

new(): failed: load(): couldn't thaw() data using CGI::Session::Serialize::default:thaw(): couldn't thaw. syntax error at (eval 253) line 2, near &quot;/&gt;&quot; 

我还在 CGI::Session.pm 中进行了窥探,发现了这个错误的抛出位置,我猜它无法解析 _DATA 甚至无法读取它......因为一些奇怪的原因字符...“/>”

CGI::Session.pm

....
    $self->{_DATA} = $self->{_OBJECTS}->{serializer}->thaw($raw_data);
    unless ( defined $self->{_DATA} ) {
        #die $raw_data . "\n";
        return $self->set_error( "load(): couldn't thaw() data using $self->{_OBJECTS}->{serializer} :" .
                                $self->{_OBJECTS}->{serializer}->errstr );
    }

知道为什么这不起作用吗?

I tried using the CGI::Session Library but for some reason my code won't keep a persistant session ... 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...

UPDATED CODE

My::Role::PersistantData

package My::Role::PersistsData;

use Moose::Role;
use namespace::autoclean;
  has '_cgi' => (
           is        => 'rw', 
           isa       => 'Maybe[CGI]', 
           builder   => '_build_cgi'
  );

  has '_sss' => (
           is        => 'rw', 
           isa       => 'Maybe[CGI::Session]',
           builder   => '_build_sss'
  );

My::Session

    package My::Session;

    use Moose;
    use namespace::autoclean;
    with 'My::Role::PersistsData';  

    use CGI;
    use CGI::Session ('-ip_match');
    use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;

    sub start{
      my($self) = @_;
      my $cgi = $self->cgi();
      $self->log("Session Started!");        
    }


    sub cgi{
      my($self) = @_;
      $self->_cgi = $self->_build_cgi() unless $self->_cgi;
      return ($self->_cgi); 
    }


    sub _build_cgi{
      my($self) = @_; 
      my $cgi = CGI->new();

      if(!$cgi){
        #print "mising cgi";          
      }

      return ( $cgi );
    }


    sub _build_sss{
          my($self) = @_;
          my $cgi = $self->cgi(); 

          my $sid = $cgi->cookie("CGISESSID") || $cgi->param('CGISESSID') || undef;

          $self->log("Session ID Initial is: ".($sid?$sid:"undef"));

          my $sss =  CGI::Session->new(undef, $cgi, {Directory=>'tmp'})  or die CGI::Session->errstr;



          my $cookie = $cgi->cookie(CGISESSID => $sss->id() );


          $self->log("Resulting Session ID is: ".$sid." cookie is: ".$cookie);    

          print $cgi->header( -cookie=>$cookie );

          return ( $sss );
    }

main.pl

use Data::Dumper; 
$Data::Dumper::Sortkeys = 1;

use CGI;
use CGI::Carp qw(fatalsToBrowser);
use My::Session;

$| = 1;
$, = " ";
$\ = "\n <br />";

  my $sss = My::Session->new();      
  $sss->start();
  print Dumper($sss);

It'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...

however if I load the page again, suddenly the $sss (session) comes back as undefined, when it should return a new Session object:

 $sss = new CGI::Session("driver:File", $sid, {Directory=>'/tmp'})

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:

new(): failed: load(): couldn't thaw() data using CGI::Session::Serialize::default:thaw(): couldn't thaw. syntax error at (eval 253) line 2, near "/>" 

I also snooped around in CGI::Session.pm and found where this error was being thrown at, I guess it's not able to parse _DATA or even read it...because of some strange characters... "/>"

CGI::Session.pm

....
    $self->{_DATA} = $self->{_OBJECTS}->{serializer}->thaw($raw_data);
    unless ( defined $self->{_DATA} ) {
        #die $raw_data . "\n";
        return $self->set_error( "load(): couldn't thaw() data using $self->{_OBJECTS}->{serializer} :" .
                                $self->{_OBJECTS}->{serializer}->errstr );
    }

Any idea why this isnt working?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泪冰清 2024-12-12 03:20:00

这很可能是由于发送了不同的会话 cookie(在那里,用头撞墙。很难)。

请打印最初用于存储会话的会话 cookie 值以及后续请求提供的会话 cookie 值。

如果它们确实不同,您有 2 个选择:

  • 调查浏览器在后续请求中发送不同会话 cookie 的原因,并以某种方式解决该问题。

    我从来没有找到确凿的答案,但我的应用程序由一个带有内部 的框架组成,所以我怀疑这是由于这个原因。

  • 如果你像我一样找不到根本原因,也可以解决这个问题。

    我的解决方法:将原始会话 cookie 值显式存储为表单变量,并在大约 100% 的代码片段中传递。

    然后在服务器端代码请求会话数据之前使用正确的 cookie 值重新初始化会话对象。

    不太安全、烦人、难以正确处理。但有效。我不会推荐它,除非作为最后的手段黑客

Most likely this is due to a different session cookie being sent (been there, hit that wall with head. HARD).

Please print the session cookie value being used to store the session initially as well as session cookie value supplied by subsequent request.

If they are indeed different, you have 2 options:

  • Investigate why different session cookie is sent by the browser in subsequent requests and fix that issue somehow.

    I was never able to find conclusive answer but my app consisted of a frame with internal <iframe> so I suspect it was due to that.

  • If like me you can't find the root cause, you can also work around this.

    My workaround: explicitly STORING the original session cookie value as a form variable being passed around 100% of your code pieces.

    Then re-initialize session object with correct cookie value before your server side code requests session data.

    Not very secure, annoying, hard to get right. But works. I wouldn't recommend it except as a uber-last-resort hack

漫漫岁月 2024-12-12 03:20:00

也许您可以尝试(或者至少查看代码以了解它是如何工作的)一些有状态的 webapp 模块。我使用过 Continuity,非常酷的东西。

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.

生生漫 2024-12-12 03:20:00

由于某种原因,您无法使用 Data::Dumper 或其他带有 CGI::Session

找到答案 此处此处 perlmonks.org/?node_id=648246" rel="nofollow">这里

删除 Dumper 和 HTML 输出解决了这个问题 -- 有点 --

更新

显然你必须使用转义

 $cgi->escapeHTML ( Dumper($session) );

最终解决了这个问题。

Perl 很痛苦!

For some reason you can't use Data::Dumper or other HTML tags with CGI::Session

Answer found here and here

Removing Dumper and HTML output fixed this problem -- kind of --

updated

Apparently you have to use escapes

 $cgi->escapeHTML ( Dumper($session) );

and that FINALLY resolves this problem.

Perl is a pain!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文