in reply to DBI & MySQL Login Test
I experienced cases where placeholders didn't work depending on the OS, type and version of DB client libs, DB server software and following SQL statements. Here is my solution, assuming you already checked that both $uname and $pword are not empty:
What happens?if ($sth->selectrow_array('SELECT COUNT(*) FROM '.$Tablename. 'WHERE username = 0x'.unpack('H*',$uname). ' AND password = 0x'.unpack('H*',$pword))) { # Successful login } else { # Oops, something failed }
You'll notice that no SQL injection is possible any longer, because everything which is read from the user is converted to hex which couldn't harm the SQL string.SELECT COUNT(*) FROM users WHERE username = 0x75736572 AND password = +0x313233
Last note: If you've got other things to fetch from the user DB, you could also combine this:
my ($UserID,$UserState) = $sth->selectrow_array('SELECT ID,State FROM +'.$Tablename. 'WHERE username = 0x'.unpack('H*',$uname). ' AND password = 0x'.unpack('H*',$pword)); if ( ! defined($UserID)) { # Oops, no UserID means no user with matching username/password was f +ound } elsif ($UserState eq 'LOCKED') { # Oops, this user is not allowed to login } else { # ok, everything right, the user is validated }
(Everything shown here is based on Sybase SQL products, but should also work on mySQL and other SQL servers. As I don't use mySQL mysqlf, I can't test it, sorry.)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: DBI & MySQL Login Test
by astroboy (Chaplain) on Aug 25, 2009 at 22:16 UTC | |
by Sewi (Friar) on Aug 26, 2009 at 12:29 UTC | |
by astroboy (Chaplain) on Aug 26, 2009 at 14:59 UTC | |
by astroboy (Chaplain) on Aug 26, 2009 at 15:24 UTC | |
|
Re^2: DBI & MySQL Login Test
by swordfish (Initiate) on Aug 26, 2009 at 03:28 UTC | |
by Sewi (Friar) on Aug 26, 2009 at 12:37 UTC |