0
votes

It's my first experiment with CGI::Sessions and I'm getting an error message in my logs that I don't know how to fix. It looks like the problem is on line 28 of my code, the $cookie variable in not imported:

[Mon Aug 18 21:42:32 2014] [error] [client 127.0.0.1] Variable "$cookie" is not imported at /usr/lib/cgi-bin/login.pl line 28. 5028 [Mon Aug 18 21:42:32 2014] [error] [client 127.0.0.1] \t(Did you mean &cookie instead?) 5029 [Mon Aug 18 21:42:32 2014] [error] [client 127.0.0.1] Variable "$cookie" is not imported at /usr/lib/cgi-bin/login.pl line 29. 5030 [Mon Aug 18 21:42:32 2014] [error] [client 127.0.0.1] \t(Did you mean &cookie instead?) 5031 [Mon Aug 18 21:42:32 2014] [error] [client 127.0.0.1] Global symbol "$cookie" requires explicit package name at /usr/lib/cgi-bin/login.pl line 28. 5032 [Mon Aug 18 21:42:32 2014] [error] [client 127.0.0.1] Global symbol "$cookie" requires explicit package name at /usr/lib/cgi-bin/login.pl line 29. 5033 [Mon Aug 18 21:42:32 2014] [error] [client 127.0.0.1] Bareword "username" not allowed while "strict subs" in use at /usr/lib/cgi-bin/login.pl line 34. 5034 [Mon Aug 18 21:42:32 2014] [error] [client 127.0.0.1] Bareword "password" not allowed while "strict subs" in use at /usr/lib/cgi-bin/login.pl line 35. 5035 [Mon Aug 18 21:42:32 2014] [error] [client 127.0.0.1] Execution of /usr/lib/cgi-bin/login.pl aborted due to compilation errors. 5036 [Mon Aug 18 21:42:32 2014] [error] [client 127.0.0.1] Premature end of script headers: login.pl

Here's my code:

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 $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  }   
1

1 Answers

1
votes

As you're using strict in your code (which is, of course, a great habit to get in to), you'll need to declare all of your variables with my (or something equivalent). You don't do that for $cookie.

So change:

$cookie = $cgi->cookie(CGISESSID => $session->id);

To:

my $cookie = $cgi->cookie(CGISESSID => $session->id);

For future reference, if you get a Perl error message that you don't understand, you can get more detail about the problem by adding use diagnostics to you code (but remember to remove it again once the problem is fixed).

You also have a couple of other errors. The strings 'username' and 'password' need to be quoted string on lines 34 and 35.