For the life of me I cannot figure out why the below snippet fails. After the user submits the form it checks their username and password and checks in the in the database. But even with the correct login (username is "admin" password is "pass", it says the login failed.

The creation of that table is

my $sth = $dbh->prepare( "CREATE TABLE IF NOT EXISTS admin ( id int auto_increment not null, username VARCHAR(25) DEFAULT 'admin' NOT NULL, password VARCHAR(36) DEFAULT '1a1dc91c907325c69271ddf0c944bc72' + NOT NULL, primary key (id) )"); $sth->execute(); if ($sth->errstr) { print $sth->errstr; } else { print "Admin table was setup"; }
The login script itself is below
#!/usr/bin/perl use warnings; use strict; use CGI::Carp 'fatalsToBrowser'; use CGI qw/:standard/; use Digest::MD5 qw(md5_hex); use CGI::Cookie; use DBI; #################### # Configuration section #################### my $dbase = "spyders_test"; my $mysql_user = "spyders_admin"; my $mysql_pass = "pass"; #################### # Do NOT edit below this line #################### ###### # Connecting to our database ###### my $dbh = DBI->connect("DBI:mysql:$dbase", $mysql_user, $mysql_pass) o +r print DBI=>"errstr"; ####### # Check to see if they have a login cookie ####### my %cookies = fetch CGI::Cookie; if (defined($cookies{'user_id'})) { print header, start_html("Login"); print "You are already logged in. Please wait while you're redirec +ted..."; print "<script>window.location = 'index.cgi';</script>\n"; exit; } ####### # The form was submitted, let's process it ####### if (param()) { my $username = param("username"); my $password = param("password"); $password = md5_hex($password); my $data = qq(SELECT * FROM admin WHERE username = "$username" AND p +assword = "$password"); my $sth = $dbh->prepare($data); $sth->execute() or die $dbh->errstr; if ($sth->rows < 1) { print header, start_html("Authentication Failed"); print "User was $username: password was $password<p>"; print "<b>Your username or password were incorrect.</b> <p>Please + click Back to continue</p>"; exit; } else { while ($data = $sth->fetchrow_hashref) { my $auth_user = new CGI::Cookie(-name => 'user_id', -value + => $username); my $auth_pass = new CGI::Cookie(-name => 'user_pass', -value + => $password); print "Set-Cookie: $auth_user\n"; print "Set-Cookie: $auth_pass\n"; print header, start_html("Login"); print "Welcome " . $username . ", you have successfully logged in +.\n"; print "<script>window.location = 'admin.pl';</script>\n"; } } }
Anyone have any ideas why it may be failing? I know the form fields are ligned up because doing a test print I get
User was admin: password was 1a1dc91c907325c69271ddf0c944bc72 Your username or password were incorrect. Please click Back to continue
Thank you

In reply to faulty small cgi/mysql login by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.