Chinaunix
标题:
CGI::Session和redirect
[打印本页]
作者:
兰花仙子
时间:
2010-02-01 19:08
标题:
CGI::Session和redirect
看刚才那个帖子,有点angry和郁闷,莫非是偶讲的不够清楚?
OK let's rego,重走一遍流程。
假设用户需要登录,登录页面的代码如下:
# cat welcome.cgi
#!/usr/bin/perl
use strict;
use CGI;
use CGI::Session;
require 'session.pl';
my $q = CGI->new;
my $session = fetch_session();
print $session->header(-type=>'text/html');
print <<HTML;
<html>
<head>
<title>welcome</title>
</head>
<body>
<form action = "/cgi-bin/login.cgi" >
Please enter your username and password.
<br>
<br>
username:<input type="text" name="user"> <br>
password:<input type="password" name="pass">
<br>
<input type="submit"/>
</form>
</body>
</html>
HTML
复制代码
在用户打开welcome.cgi时,脚本就创建了一个session,并将sid发送到了客户端。
然后用户进行登录,login.cgi代码如下:
# cat login.cgi
#!/usr/bin/perl
use strict;
use CGI;
use CGI::Session;
require 'session.pl';
my $q = CGI->new;
my $session = fetch_session();
if ($q->param('user') eq 'guest' and $q->param('pass') eq '12345') {
$session->param('login',1);
} else {
$session->param('login',0);
}
print $q->redirect("afterlogin.cgi");
复制代码
login.cgi在接收到正确的用户名和密码后,会在session里保存登录状态为1,否则为0.
然后,执行一个redirect,跳转到afterlogin.cgi.
# cat afterlogin.cgi
#!/usr/bin/perl
use strict;
use CGI;
use CGI::Session;
require 'session.pl';
my $q = CGI->new;
my $session = fetch_session();
print $session->header(-type=>"text/plain");
my $logined = $session->param('login');
print "logined: $logined";
复制代码
afterlogin.cgi简单的从session里读取登录状态,并print到浏览器。
整个过程session都是正确执行的。session依赖于一个sid,这个sid既发往客户端,又在服务器上保存。
当welcome.cgi创建了session并传递sid给浏览器后,浏览器在后续请求里,都会带上这个sid.
如下session.pl演示了这个session的创建/加载过程:
# cat session.pl
sub fetch_session {
my $session = CGI::Session->load() or die CGI::Session->errstr;
if ( $session->is_expired or $session->is_empty ) {
$session = new CGI::Session() or die CGI::Session->errstr;
$session->expire('+30m');
}
$session;
}
1;
复制代码
在创建好上述几个脚本并设置权限后,就可以通过:
http://1.2.3.4/cgi-bin/welcome.cgi
来访问。在输入guest和12345后,可以看到页面输出logined: 1,否则是logined: 0.
I hope this helps.
否则不是你疯就是俺crazy了。
作者:
maybenot
时间:
2010-04-06 14:34
我照着作的怎么也不行?
在命令窗口里面显示是好的:
C:\Program Files\Apache Software Foundation\Apache2.2\cgi-bin>perl welcome.cgi
Set-Cookie: CGISESSID=608ecefc35cbee406a1df284d91908e2; path=/; expires=Tue, 06-Apr-2010 06:54:54 GMT
Date: Tue, 06 Apr 2010 06:24:54 GMT
Content-Type: text/html; charset=ISO-8859-1
<html>
<head>
<title>welcome</title>
</head>
<body>
<form action = "/cgi-bin/login.cgi" >
Please enter your username and password.
<br>
<br>
username:<input type="text" name="user"> <br>
password:<input type="password" name="pass">
<br>
<input type="submit"/>
</form>
</body>
</html>
复制代码
可是浏览器里面就不行了, 用了use CGI::Carp qw(warningsToBrowser fatalsToBrowser);也没信息,只是说Internal Server Error,
access.log里面:
127.0.0.1 - - [06/Apr/2010:14:25:27 +0800] "GET /cgi-bin/welcome.cgi HTTP/1.1" 500 540
error.log里面:
[Tue Apr 06 14:25:27 2010] [error] [client 127.0.0.1] (OS 3)The system cannot find the path specified. : couldn't spawn child process: C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin/welcome.cgi
怎么回事?文件名没弄错。。
作者:
maybenot
时间:
2010-04-06 14:50
第一句话都没改。。太挫了
C:/perl/bin/perl.exe
作者:
haolian102
时间:
2010-04-08 13:58
都怪我发帖前没看论坛其它贴,唉,早知道就不发了。
我一般使用session保存登录后的用户组别,然后根据组别决定用户权限,很方便,
但是我一直害怕别人可以在这里做什么手脚,越过登录步骤直接在session里面写入组别来访问后面的数据,
这样后面的页面没有到DB里验证,直接给了session里的权限,我感觉有点不太安全。
目前是不是有这种技术可以自己建立一个session,直接访问afterlogin页面?
作者:
haolian102
时间:
2010-04-08 14:00
还有一个问题,我的页面都是.pl后缀的,当然用.CGI也可以,为何大家都喜欢用.CGI做后缀呢?
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2