I have two files, one is the login from which I get user credentials and I want to forward the user to another page if he passes authentication using CGI::Sessions. The problem is that I am missing something in the hello.pl file. I looked online for code examples but couldn't find any with two or more files. What should I put in my code in hello.pl in order to redirect using sessions so the variable username can be pulled out from hello.pl
login.pl:
1 #!/usr/bin/perl -wT
2 use strict;
3
4 use CGI::Session;
5 use CGI qw(:standard);
6
7 my %names = map { $_ => 1 } param;
8 my $open = "1234";
9
10 sub windowLayout() {
11 header,
12 start_html("Input Form"),
13 start_form,
14 "Please enter your username:",
15 textfield(-name=>'username',
16 -maxlength=>20),p,
17 "Please enter your password:",
18 password_field(-name=>'password',
19 -maxlength=>20),p,
20 submit,hr
21 }
22
23 if ($names{username} and $names{password}) {
24
25 my $cgi = new CGI;
26 my $session = new CGI::Session(undef, $cgi, {Directory=>'/tmp'});
27
28 my $cookie = $cgi->cookie(CGISESSID => $session->id);
29 print $cgi->header(-cookie=>$cookie);
30
31 my $username = param("username");
32 my $password = param("password");
33
34 $session->param('username', $username);
35 $session->param('password', $password);
36
37 if ($password ne $open) {
38 print
39 windowLayout(),
40 end_form,
41 p({-style=>'Color: red;'},
42 "Sorry wrong password!");
43 print end_html;
44 } else {
45 print
46 redirect("hello.pl");
47 }
48 }else {
49 print
50 windowLayout(),
51 end_html;
52 }
hello.pl
#!/usr/bin/perl -wT
2 use strict;
3
4 use CGI::Session;
5 use CGI qw(:standard);
6
7 #my $cgi = CGI->new;
8 #my $usrname = $cgi->param("username");
9
10 my $username = $session->param("username");
11
12 print
13 header,
14 start_html (-BGCOLOR=>'green'),
15 "Hello, $usrname",p,
16 "The time now is: ",
17 scalar(localtime),
18 # h1({-style=>'Color: orange;'}, "$usrname you win \$1,000,000!");
19 end_html;
I recommend looking at the following question for an additional example on using
CGI::Session
:However, your two scripts can be cleaned up to work at transferring the username from one to the other.
The biggest thing I would recommend is setting your
CGI
object andCGI::Session
object first thing. This is true of both of your scripts and any other cgi style scripts that you would create:login.pl
hello.pl