两个Perl脚本之间的cgi会话 [英] cgi sessions between two Perl scripts
问题描述
在我的网站上使用两个Perl脚本。我在侧面有一个搜索字段,该字段调用第一个脚本,结果写在输出文件中。
下次单击时,用户将调用第二个脚本,该脚本将读取输出文件并基于用户搜索来构建配置文件。
目前我仅在输出文件上写入,但是我需要使用会话ID来个性化此文件。
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 invokes the second Script, this one reads the output file and builds a config File based on the user search. At the moment im only writing on output file, but i need to personalise this file with a session id.
我正在尝试使用CGI :: Session,但是我不理解如何使用这两个Perl脚本。我想我需要比较会话ID,并为每个会话编写一个输出文件。
Im trying this with CGI::Session, but I don’t understand how to to this, with these two Perl Scripts. I think i need to compare the session ids, and write a output File for every Session.
你们中的一个知道如何做到吗?
Does one of you know how to do this??
第一个脚本:
Im在文件中写入会话:
First Script: Im Writing the Session in a file:
my $session = new CGI::Session(undef, $cgi, {Directory=>"/usr/local/path/"});
然后将cookie发送给客户端:
Then sending a cookie to the client:
my $cookie = $cgi->cookie(CGISESSID => $session->id );
第二个脚本:
尝试获取会话ID:
Second Script: try to get the Session ID:
$sid = $cgi->cookie("CGISESSID") || undef;
print $sid
它给出状态:500
。
推荐答案
您需要以将客户端提供会话ID的方式将会话ID传递给客户端在以后的请求中返回给您。
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).
这篇关于两个Perl脚本之间的cgi会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!