0

In my main script, main.cgi, I present the user with a form to login. When this form is submitted it, another script is called to perform the verification, login.cgi.

login.cgi

# Convert request method to Uppercase eg: GET, POST
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;

# If "POST" request sent, check login credentials
if ("$ENV{'REQUEST_METHOD'}" eq "POST"){
    # Get login parameters
    my $username = param('username');   
    my $password = param('password');   

    my $loginCheckResult = check_login($username, $password);

    # If login was successful, create a cookie
    if ($loginCheckResult){
        # Set Cookie
        my $cookie = CGI::Cookie->new(-name=>'USERID',-value=>$cookie_value);       
        print redirect(-uri => '/cgi-bin/main.cgi/', -cookie => $cookie);   


     # If login was Unsuccessful, redisplay the login page
     } else {
          # Do something here...    

     }
 }

If the login is successful, I create a cookie and send the user back to the main page. Here I can test if a cookie is existent to determine whether the login was successful.

However, I'm not sure what to do if the login was unsuccessful.

main.cgi

if ($cookie eq ""){
     print show_login_form();
# Login successful
} else{ 
     print $cookie;
}

If I redirect them back to main.cgi, the login form will be showed again but no error will be displayed. If I include an error message underneath

print show_login_form();

then it will always be showed. Is there a way that I could send back a variable to indicate that the login failed and then check for this variable in main.cgi?

Or should I just create another login form in login.cgi upon an unsuccessful login attempt and include an error message in this form?

Thank you for your help.

  • 1
    Keep your Form and your Validation in the same Script. That way when your validation fails, you can return your same form with error messages include. The following question and answer demonstrates: [Why doesn't CGI::Sessions work?](http://stackoverflow.com/a/25400717/1733163) – Miller Oct 24 '14 at 21:47
  • Thanks Miller. I have done something similar to what you stated in this post. –  Oct 24 '14 at 23:53

1 Answers1

0

The code in the 'successful login' code path is the code that generated the page a logged-in user should see.

Len Jaffe
  • 3,442
  • 1
  • 21
  • 28