cgi sessions between two Perl scripts

后端 未结 1 1377
不思量自难忘°
不思量自难忘° 2021-01-26 04:59

im using two Perl Scripts in my Website. I got a search field on the side, which invokes the first Script, the result is written in a output File. With the next click, the user

相关标签:
1条回答
  • 2021-01-26 05:43

    You need to pass the session id to the client in a way that the client will provide it back to you in later requests. This is usually done using cookies.

    use utf8;
    use open ':std', ':encoding(UTF-8)';
    
    use CGI          qw( -utf8 );
    use CGI::Session qw( );
    
    use constant SESSION_DIR    => '...';
    use constant SESSION_EXPIRE => '+12h';  # From last use (not creation)
    
    my $cgi = CGI->new();
    
    my $session = CGI::Session->new(
       'driver:file',
       scalar($cgi->cookie('session_id')),
       { Directory => SESSION_DIR },
    );
    $session->expire(SESSION_EXPIRE);
    
    # Whatever your page does
    my $count = $session->param('count');
    ++$count;
    $session->param(count => $count);
    my $content = "This is request $count";
    
    print
       $cgi->header(
          -type    => 'text/html',
          -charset => 'UTF-8',
          -cookie => $cgi->cookie(
             -name => 'session_id',
             -value => $session->id,
             -expires => SESSION_EXPIRE,
           ),
       ),
       $content;
    

    There's only one script shown here, but there's obviously nothing stopping two scripts from accessing the same session (if they can access the same session store).

    0 讨论(0)
提交回复
热议问题