我有一个带有字段的 Emp(类)对象name
,id
并且password
:
$emp = new Emp({name=>'pavan', id=>101, password=>'05cc11'});
$serializble = freeze($emp);
存储在会话中:
$session->param("emp",$serializble);
但是当我打开存储在 tmp 目录中的会话对象时,emp 值为undef
.
更新
使用 Storable 序列化对象后更新。
$serializble = freeze($emp);
$session->param("emp",$serializble);
这是会话文件(添加了换行符以提高可读性):
$D = {'_SESSION_ID' => 'dd75c6042893334a6bf26794b4ce5c74',
'_SESSION_ATIME' => 1356628765,
'emp' => '^D^G^D1234^D^D^D^H^Q^CPEmp^C^B^@^@^@ ^Epavan^D^@^@^@name^H�^B^@^@^@id',
'_SESSION_REMOTE_ADDR' => '127.0.0.1',
'_SESSION_CTIME' => 1356628765};;$D
当我尝试从会话中返回对象时,它返回undef
:
$recoverable = thaw($session->param('emp');
print $recoverable;
这是我的总代码
Emp类:
package Emp;
sub new{
my ($class, $args) = @_;
my $self = bless{}, $class;
$self->{name} = $args->{name};
$self->{id}=$args->{id};
return $self;
}
sub getEmpname{
my $self = shift;
return $self->{name};
}
1;
emp.cgi
$query = new CGI();
$session = new CGI::Session("driver:File", undef, {Directory => "/tmp"});
$emp = new Emp( { name => $query->param('username'),
id => 101
} );
my $serialized = freeze($emp);
$session->param("emp", $serialized);
$login = $emp->getEmpname(); #it is the method of Emp class
$cookie = $query->cookie( -name => $login,
-value => $session->id,
-expires => '+3M',
);
print $query->header( -type => 'text/html',
-cookie => $cookie,
);
欢迎.cgi
$q = new CGI();
$sid = $q->cookie('login') || $q->param('login') || undef;
$session = new CGI::Session(undef, $sid, {Directory=>'/tmp'});
print $q->header('text/html');
print $q->start_html('title');
print "<br>";
print Dumper $session->param('emp');
my $emp = thaw( $session->param('emp') ); //which is saved in session object.
print $emp->getEmpname();
print end_html;