I am trying to implement a basic user authorisation system using perl and CGI. The browser (firefox & safari) does not react to the CGI redirect (upon successful login to a new page) or even to a simple print HTML after including jQuery 1.11.13.min.js in the header of the HTML login page. The browser's content area and url textbox remain the same as CGI script were never executed. But I know the scripts were executed.
Here is a summary of the system:
The user is first presented with an HTML login page (login_page.html) which contains a basic login form with 'username' and 'password' fields, the form action is a cgi script (verify.cgi) called using POST.
This script checks if credentials are OK and either redirects to a new CGI script (welcome.cgi) which prints a basic welcome message in HTML or just prints a basic html message of failure there and then (all three files are included further down this post).
However, there is a problem after redirecting to the welcome (CGI) page. The welcome.cgi script is called successfully because I made it print a message in a file in the server. BUT the url-address box of the browser (firefox 41.0.2 and safari 6.2.4) is not updating, the http://..../verify.cgi url remains there. Also, the browser displays the login page html and does not update its content area to the welcome or failed login message html output-ed by the verify.cgi/welcome.cgi scripts.
The problem appears after including the standard jQuery js file (http://code.jquery.com/jquery-1.11.3.min.js) in the header of my login page (in html).
Can anyone help me finding out what I am doing wrong and how to fix it?
Please do not ask why I want to include jQuery. And I prefer to stick with perl than anything else.
General comments about the code and design welcomed.
Many thanks
Here are the three files in their most basic form:
login_page.html
<!doctype html>
<html>
<head>
<!-- this one creates a problem with CGI redirect -->
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<!-- this one does not seem to create any problem -->
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>
<form action="/bin/verify.cgi" method="post" name="login_form">
<input type="text" name="username" id="username"><br/>
<input type="password" name="password" id="password"><br/>
<input type="submit" value=" Log In " name="login_button"/>
</form>
</body>
</html>
verify.cgi
#!/usr/bin/env perl
use strict;
use warnings;
use CGI::Carp;
use CGI::Session;
use CGI;
my $cgi = CGI->new;
my $username = $cgi->param("username");
my $password = $cgi->param("password");
if( defined($username) &&
defined($password) &&
($username eq 'test') &&
($password eq 'test')
){
# successful login
my $session = CGI::Session->new(
"driver:File",
undef,
{Directory=>'/tmp'}
);
my $sid = $session->id();
$session->param('username', $username);
# setup the cookie to the session
my $cookie = $cgi->cookie(
-name => 'CGISESSID',
-value => $sid
);
$session->save_param();
$session->flush();
# move on to the next page and place the cookie in the headers
# the script in the -location is EXECUTED OK - so redirect works
# however browser does not update URL textbox nor its content page:
print $cgi->redirect(
-cookie => $cookie,
-url => 'http://c3.myartsonline.com/bin/welcome.cgi'
);
# even with this it does not work
# print $cgi->header() . "<html><body>welcome, login successful</body></html>";
} else {
# login failed
# this I can not see in the browser either
print $cgi->header() . '<html><body>login failed</body></html>';
}
1;
__END__
welcome.cgi
#!/usr/bin/env perl
use strict;
use warnings;
use CGI;
use CGI::Session;
my $cgi = CGI->new;
my ($session, $username) = (undef) x 2;
my $sid = $cgi->cookie('CGISESSID') || $cgi->param('CGISESSID') || undef;
if( defined($sid) ){
$session = CGI::Session->new(undef, $sid, {Directory=>'/tmp'});
if( defined($session) ){
$username = $session->param('username')
}
}
if( defined($username) ){
print $cgi->header() . "<html><body>welcome user '$username', login successful</body></html>";
open(OUT, ">hello");
print OUT "weclome\n";
close(OUT);
} else {
print $cgi->header() . "<html><body>please login first</body></html>";
}
1;
__END__
redirectmethod as described in the CGI.pm documentation under Generating a redirection header. You can see how the headers differ from your current method by running on the command line: compareperl -MCGI -e'print CGI->new->header(-location => "foo")'toperl -MCGI -e'print CGI->new->redirect("foo")'- ThisSuitIsBlackNotCGI::redirect()rather than withCGI::location()as per ThisSuitIsBlackNot's correct suggestion. The problem still persists. - bliako