你如何让CGI :: Session在一定间隔后过期?

时间:2011-10-14 02:17:13

标签: perl session cgi expired-sessions

似乎CGI :: Session expire()函数仅在用户空闲指定间隔后才会过期。我假设他们意味着用户没有刷新页面或访问任何其他页面。

虽然我看到你可以通过使用delete()来强制会话到期,但我没有看到的是一种自动强制会话到期的方法,无论用户是否空闲。

也许这不是一个理想的用户体验,但为了便于理解,有没有办法在不必跟踪时间间隔或使用任何其他库的情况下执行此操作?

如果会话在过期时返回一个新的,那么CGI :: Session :: is_expired的重点是什么?至少我似乎无法使用我的脚本

进入过期状态
  sub build_sss{
    my($this) = shift;

    $cgi = $this->cgi_obj();

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

   #check expired session
    if ( $sss->is_expired() ) {
      my $expired_url = $ENV{'SCRIPT_URI'}.'?expired='.$sid;
      $this->session_status("SESSION_EXPIRED");
      print $cgi->redirect($expired_url); #when expired param is set shows a msg    
    }


    #check if empty create new
    if ( $sss->is_empty() ) {     
      $sss = $sss->new() or $sss->errstr;
      $this->session_status("SESSION_NEW");
      $sss->expire('30s');
      $sss->expire(_TEST_SUB_SESSION => '15s');
    }

    return $sss;
  }

2 个答案:

答案 0 :(得分:1)

更新:所以是的,如果你想根据创建时间让会话过期,你必须继承CGI :: Session

1)确保您拥有最新版本的CGI :: Session 2)阅读CGI :: Session :: Tutorial 3)编写证明你的主张的程序,如CGI::Session expire demo

#!/usr/bin/perl --

use strict;
use warnings;

use CGI();
use CGI::Session();

my ($oneid);
{
  my $one = CGI::Session->new or die CGI::Session->errstr;
  $one->expire('3s');
  $one->param(qw' var value ');
  $oneid = $one->id;

  print "set exire to 3 seconds\n";
}

for my $loop ( 1 .. 4 ) {
  sleep 1;
  my $bob = CGI::Session->load($oneid) or die CGI::Session->errstr;
  print "one second later $bob / $oneid load\n";
}

for my $loop ( 1 .. 4 ) {
  sleep 2;
  my $bob = CGI::Session->load($oneid) or die CGI::Session->errstr;
  print "two seconds later ";
  if ( $bob->is_expired ) {
    print "$bob / $oneid is_expired\n";
  } else {
    print "var=", $bob->param('var'), "\n";
  }
} ## end for my $loop ( 1 .. 4 )

{
  sleep 3;
  my $bob = CGI::Session->load($oneid) or die CGI::Session->errstr;
  print "three seconds later  ";
  if ( $bob->is_expired ) {
    print "$bob / $oneid is_expired\n";
  } else {
    print "var=", $bob->param('var'), "\n";
  }
}

__END__



set exire to 3 seconds
one second later CGI::Session=HASH(0xa965fc) / cf27e3ec9ff5a06a5bef4491e830c8b6 load
one second later CGI::Session=HASH(0x97a164) / cf27e3ec9ff5a06a5bef4491e830c8b6 load
one second later CGI::Session=HASH(0xbef68c) / cf27e3ec9ff5a06a5bef4491e830c8b6 load
one second later CGI::Session=HASH(0xbef56c) / cf27e3ec9ff5a06a5bef4491e830c8b6 load
two seconds later var=value
two seconds later var=value
two seconds later var=value
two seconds later var=value
three seconds later  CGI::Session=HASH(0xa965ec) / cf27e3ec9ff5a06a5bef4491e830c8b6 is_expired

答案 1 :(得分:0)

来自文档:

“is_empty() - 对于空的会话返回true。它是测试所请求的会话是否成功加载的首选方法”

我不明白你是如何尝试在会话空的情况下使会话失效?

#check if empty create new
if ( $sss->is_empty() ) {     
  $sss = $sss->new() or $sss->errstr;
  $this->session_status("SESSION_NEW");
  $sss->expire('30s');
  $sss->expire(_TEST_SUB_SESSION => '15s');
}

你的意思是:

if ( !$sss->is_empty() ) { #...Not empty?