Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#ffcccc
langperl
use warnings;
use strict;
use CGI qw(:standard);

print header;
print start_html('A Simple Example'),
  h1('A Simple Example'),
  start_form,
  "What's your name? ",textfield('name'),
  submit,
  end_form,
  hr;

if (param()) {
  print "Your name is: ",em(param('name')),
    hr;
}
print end_html;

...

Code Block
bgColor#ffcccc
langperl
use DBI;
use warnings;
use strict;
 
my $dbfile = "users.db";
my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile","","")
  or die "Couldn't connect to database: " . DBI->errstr;
 
sub hash() {
  # hash the password
}
 
print "Enter your id: ";
my $userid = <STDIN>;
chomp $userid;
print "Enter your password: ";
my $password = <STDIN>;
chomp $password;
my $hashed_password = hash( password);
my $sth = $dbh->prepare("SELECT * FROM Users WHERE userid = '$userid' AND password = '$hashed_password'")
  or die "Couldn't prepare statement: " . $dbh->errstr;
$sth->execute($userid, $hashed_password)
  or die "Couldn't execute statement: " . $sth->errstr;
 
if (my @data = $sth->fetchrow_array()) {
  my $username = $data[1];
  my $id = $data[2];
  print "Access granted to user: $username ($userid)\n";
}
 
if ($sth->rows == 0) {
  print "Invalid username / password. Access denied\n";
}
 
$sth->finish;
$dbh->disconnect;

...

Code Block
bgColor#ccccff
langperl
use DBI;
use warnings;
use strict;
 
my $dbfile = "users.db";
my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile","","")
  or die "Couldn't connect to database: " . DBI->errstr;
 
sub hash() {
  # hash the password
}
 
print "Enter your id: ";
my $userid = <STDIN>;
chomp $userid;
print "Enter your password: ";
my $password = <STDIN>;
chomp $password;
my $hashed_password = hash( password);
my $sth = $dbh->prepare("SELECT * FROM Users WHERE userid = ? AND password = ?")
  or die "Couldn't prepare statement: " . $dbh->errstr;
$sth->execute($userid, $hashed_password)
  or die "Couldn't execute statement: " . $sth->errstr;
 
if (my @data = $sth->fetchrow_array()) {
  my $username = $data[1];
  my $id = $data[2];
  print "Access granted to user: $username ($userid)\n";
}
 
if ($sth->rows == 0) {
  print "Invalid username / password. Access denied\n";
}
 
$sth->finish;
$dbh->disconnect;

...