channark;10981906 wrote:hi i try to create a login form width inscription using md5() function for password and session but when i login its not working no errors nothing, the code after the line that compar md5 db password and form password is not working. please help me.
//....
if ( mysql_num_rows($do) == 1)
{
$select_array = mysql_fetch_array($do); //this generates a numerical array
if ($select_array['pass'] == md5($pass))
{
$_SESSION['user']=$user;
$_SESSION['pass']=$pass;
header("location:index.php?log=ok");
}
}
//....
From what I can tell your using mysql_fetch_array() to generate your result array. This function generates a numerical array, so selecting your password field would be $select_array[1]. (commented line)
There are three ways of achieving the same result, although both number two and three are effectively the same thing, just two different ways of achieving the same result.
Number 1:
//....
if ( mysql_num_rows($do) == 1)
{
$select_array = mysql_fetch_array($do);
if ($select_array[1] == md5($pass)) //change to this
{
$_SESSION['user']=$user;
$_SESSION['pass']=$pass;
header("location:index.php?log=ok");
}
}
//....
Number 2:
//....
if ( mysql_num_rows($do) == 1)
{
$select_array = mysql_fetch_array($do, MYSQL_ASSOC); // change to this
if ($select_array['pass'] == md5($pass))
{
$_SESSION['user']=$user;
$_SESSION['pass']=$pass;
header("location:index.php?log=ok");
}
}
//....
Number 3:
//....
if ( mysql_num_rows($do) == 1)
{
$select_array = mysql_fetch_assoc($do); //change to this
if ($select_array['pass'] == md5($pass))
{
$_SESSION['user']=$user;
$_SESSION['pass']=$pass;
header("location:index.php?log=ok");
}
}
//....
1: The above is just a small change to your conditional, it will use the result array type (numerical indices) you had originally.
2: You are asking from a string indices array, this is one way of achieving that result. By passing another param into the mysql_fetch_array() function you can force the function to return an associative array. Which by the way you coded it originally is what you wanted.
3: By invoking mysql_fetch_assoc(); you are basically doing what number 2 is doing in another way. Both work fine.
Hope this helps.
Notes:
mysql_fetch_array()
mysql_fetch_assoc()