![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: May 2007
Posts: 52
Rep Power: 2
![]() |
mysql_real_escape
hey i need some help with this login form.
everytime i do this i get login failed even though it is in the database/table/row! Plz tell me whats wrong? Yes i already connected and selected the database. $username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$result = mysql_query("SELECT * FROM registered_members");
$row = mysql_fetch_array($result);
if ($username==$row['Username']&&$password==$row['Password'])
{
die ("Login successful");
}
else
die('Login failed'); |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Feb 2006
Location: Ohio
Posts: 93
Rep Power: 3
![]() |
Re: mysql_real_escape
You only need to use mysql_real_escape_string to quote a string for use in a query. MySQL returns strings from the database in a literal (unescaped) form. You should read about mysql_real_escape_string in the php manual.
Also, $row in your code is going to represent the FIRST user selected from the database. Your SQL query selects ALL users, instead of selecting ONLY the user identified by $username. This will result in checking the password against the wrong user most of the time if you have multiple users in the table. Example: $username = mysql_real_escape_string($_POST['username']);
$result = mysql_query("SELECT * FROM registered_members WHERE username=\"$username\"");
$row = mysql_fetch_assoc($result); // note assoc
if ($row['password'] == $_POST['password']) {
// success
} else {
// failure
}
__________________
Neeley.org |
|
|
|
|
|
#3 | |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3
![]() |
Re: mysql_real_escape
Quote:
@kishou: You can also use a loop to compare against all of the things pulled from the table, but using a WHERE clause in the SQL query like dr.p showed is what you want for this situation. (If for some reason you allow the same username multiple times, then you'll need the loop as well.)
__________________
<insert disclaimer here> <insert shameless plug for Visual Studio here> |
|
|
|
|
|
|
#4 | |
|
Programmer
Join Date: May 2007
Posts: 52
Rep Power: 2
![]() |
Re: mysql_real_escape
Quote:
|
|
|
|
|
|
|
#5 | |
|
Programmer
Join Date: Feb 2006
Location: Ohio
Posts: 93
Rep Power: 3
![]() |
Re: mysql_real_escape
Quote:
Just so that we're clear, $username contains the escaped version of $_POST['username'], which is returned by mysql_real_escape_string in your code. Whether you want to insert a string, update it, select with it, anything... if the contents of a variable (like $_POST['username']) need to go into a query/statement as a string, then you need to escape it. Read the PHP manual page for mysql_real_escape_string on php.net. It goes over what the function is for, provides examples, and has some very important security information.
__________________
Neeley.org |
|
|
|
|
|
|
#6 | |
|
Programmer
Join Date: Feb 2006
Location: Ohio
Posts: 93
Rep Power: 3
![]() |
Re: mysql_real_escape
Quote:
And mysql_fetch_array stores two sets of the requested information in memory in order to make it available by numeric and associative. Best practice is to use which one you need, unless you actually need both.
__________________
Neeley.org |
|
|
|
|
|
|
#7 | |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3
![]() |
Re: mysql_real_escape
Quote:
Out of curiosity, why is using the specific array (assoc/numeric) considered a best practice? Just because you limit the ways to access the data?
__________________
<insert disclaimer here> <insert shameless plug for Visual Studio here> |
|
|
|
|
|
|
#8 | ||
|
Programmer
Join Date: Feb 2006
Location: Ohio
Posts: 93
Rep Power: 3
![]() |
Re: mysql_real_escape
Quote:
I only knew that because I've been using MySQL daily for 8 years now.Quote:
And the PHP docs say that _array doesn't cause a significant slow down, but I have doubts about that when it comes to large amounts of data, based on some of the scripts I've worked on. In all fairness to PHP, though, it had gotten to the point where every little bit helped.
__________________
Neeley.org |
||
|
|
|
|
|
#9 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Re: mysql_real_escape
Before fetching the row, make sure it exists - if the user isn't there, you'll get 0 rows returned.
$result = mysql_query("SELECT * FROM registered_members WHERE username='$username'");
if (mysql_num_rows($row) > 0) {
$row = mysql_fetch_assoc($result);
...
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|