By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,252 Members | 1,346 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,252 IT Pros & Developers. It's quick & easy.

question of whether authentication of password and username is good

100+
P: 170
hey guys
i got the code below to authenticate users for database

Expand|Select|Wrap|Line Numbers
  1. #!perl\bin\perl
  2.  
  3. use strict;
  4. use warnings;
  5. use DBI();
  6.  
  7. my $dbh;
  8. my $sth; 
  9. my $name1;
  10. my $name2;
  11.  
  12. print STDOUT"Select the name of your database: ";
  13. my $database=<STDIN>;
  14. chop($database);
  15. print STDOUT "Enter your hostname: ";
  16. my $hostname=<STDIN>;
  17. chop($hostname);
  18. print STDOUT"Enter your username: ";
  19. my $username=<STDIN>;
  20. chop($username);
  21. print STDOUT "Enter your password: ";
  22. my $password=<STDIN>;
  23. chop($password);
  24.  
  25.  
  26. $dbh = DBI->connect("DBI:mysql:database=$database;host=$hostname","$username","$password", 
  27.                     {RaiseError => 1});
  28.  
  29.  
  30. $sth = $dbh ->prepare("SELECT * FROM `user account` WHERE Username='$username'");
  31. $sth->execute();
  32. while (my $ref =$sth->fetchrow_hashref()) {
  33. $name1 = $ref->{'Username'}, $name2 = $ref->{'Password'};}
  34. print "$name1\n";
  35.  
  36. if ($username eq "$name1" && $password eq "$name2") {
  37.   print "login successful\n";}
  38. else {
  39.  
  40.   print "login unsuccessful\n";}
  41.  
  42. $dbh->disconnect();
  43.  
u might be wondering why i need to authenticate when SQL itself can authenticate the users and password
i'm doing this as i gonna connect this to a telnet session
i created a database to categorise users according to the type of commands they can type in the telnet sessions later on
so i'm wondering if such a technique of authenfying...is it alright?
Apr 25 '08 #1
Share this Question
Share on Google+
2 Replies


KevinADC
Expert 2.5K+
P: 4,059
Should be OK like you have it.

Don't get into the habit of quoting scalars needlessly:

Expand|Select|Wrap|Line Numbers
  1. if ($username eq "$name1" && $password eq "$name2") {
better written as:

Expand|Select|Wrap|Line Numbers
  1. if ($username eq $name1 && $password eq $name2) {
It is inefficient and can create hard to find bugs.
Apr 26 '08 #2

100+
P: 170
Should be OK like you have it.

Don't get into the habit of quoting scalars needlessly:

Expand|Select|Wrap|Line Numbers
  1. if ($username eq "$name1" && $password eq "$name2") {
better written as:

Expand|Select|Wrap|Line Numbers
  1. if ($username eq $name1 && $password eq $name2) {
It is inefficient and can create hard to find bugs.
thanks a lot for your help :)
Apr 28 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.