在2个cgi脚本中登录和注销会话 [英] login and logout of sessions in 2 cgi scripts
问题描述
我需要在用户登录时存储登录会话,并在用户退出系统或会话超时时删除登录会话.我在Perl中编码.我知道我可以在Perl中使用CGI :: Session模块,但是如何确保该会话由1个cgi脚本创建并由另一个cgi脚本删除.然后,系统的所有其他页面都必须先检查会话是否存在,然后才能显示其内容.
I need to store a login session when the user is login and remove the login session when the user has logged out of the system or the session has timeout. I am coding in Perl. I know I can use CGI::Session module in Perl but how do I make sure that the session is created by 1 cgi script and removed by another cgi script. Then all other pages of the system has to check if the session exist before it can display its contents.
当前,当用户成功登录后,我正在使用以下代码创建新会话.
Currently, I am using the following code to create a new session when the user has login successfully.
my $session = CGI::Session->new();
my $CGISESSID = $session->id();
但是,如何在另一个cgi脚本中将用户从会话中注销?我怀疑我只能使用以下内容,因为其他cgi脚本中未定义$ session
However, how do I log the user out of the session in another cgi script? I doubt I can just use the following since the $session is not defined in the other cgi script
$session->delete();
$session->flush();
有什么想法吗?
推荐答案
CGI :: Session默认情况下独立于脚本.因此,您应该能够做到这一点.
CGI::Sessions are independent of script by default. So you should be able to do just that.
请不要忘记以某种方式将会话ID保留在客户端上.可以使用Cookie来完成,请参见会话 header()
. ID和会话对象将自动检索(如果保存正确).
Just don't forget to persist the session ID on the client in some way. It can be done with a cookie, see session header()
for example. The ID and the session object will be retrieved automatically (if saved properly).
如果不带任何参数调用,则$ dsn默认为driver:file; serializer:default; id:md5
If called without any arguments, $dsn defaults to driver:file;serializer:default;id:md5
和 CGI::Session::Driver::file
.
您可以配置它们以使用您喜欢的存储和设置.
You can configure them to use the store and settings that you prefer.
使用会话的基本示例 CGI脚本:
Basic example CGI script using sessions:
#!/usr/bin/perl
use strict;
use warnings;
use CGI;
use CGI::Carp qw(fatalsToBrowser); # show errors in browser
use CGI::Session;
# new query object
my $q = CGI->new();
# new session object, will get session ID from the query object
# will restore any existing session with the session ID in the query object
my $s = CGI::Session->new($q);
# print the HTTP header and set the session ID cookie
print $s->header();
# print some info
print "<pre>\n";
print "Hello!\n\n";
printf "Your session ID is: %s\n", $s->id;
printf "This sessin is: %s\n", $s->is_new ? 'NEW': 'old';
printf "Stored session 'test' value: '%s'\n", $q->escapeHTML($s->param('test'));
printf "CGI Params: %s\n", join ', ', $q->param;
# handle the form submit
if(defined $q->param('save')){
# save param value in the session
$s->param('test', $q->param('test'));
printf "Set session value: '%s'\n", $q->escapeHTML($s->param('test'));
}
elsif(defined $q->param('delete')){
# delete session
$s->delete;
print "Session will be deleted.\n";
}
print "\n</pre>\n";
# simple HTML form
printf <<'_HTML_', $q->escapeHTML($s->param('test'));
<hr/>
<form>
Session value "test": <input type="text" value="%s" name="test"/>
<button type="submit" name="save">Save Value</button>
<button type="submit" name="delete">Delete session</button>
</form>
_HTML_
# eof (linebreak needed after _HTML_)
这篇关于在2个cgi脚本中登录和注销会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!