Login without differentiating between capital letters and minusculas?

Asked

Viewed 525 times

0

I am making a login system. In the table is registered a user "User01", when the customer will log in if he puts it in the field "user01" I want it to be valid anyway.

Here’s the code I’m using.

$Name = mysqli_real_escape_string($conn, $_POST['Name']);
$Pass = mysqli_real_escape_string($conn, $_POST['Pass']);
$PassMD5 = md5($Pass);

$result1 = "SELECT * FROM users WHERE username = '$Name' && userpass = '$PassMD5' LIMIT 1";
$result2 = mysqli_query($conn, $result1);
$result3 = mysqli_fetch_assoc($result2);

if(isset($result3)){
    echo "SUCCESS";
}else{
    echo "FAIL";
}
  • 1

    Which bank is yours?

  • You are tagged, Mysql.

  • 1

    Mariadb(mysql), version PHP 7.1

3 answers

3

// Converta o nome de usuário em minúsculas
$username = strtolower($Name);

// Use LOWER () para converter o nome de usuário em minúsculas
$result1 = "SELECT * FROM users WHERE LOWER(username)='$username' && userpass = '$PassMD5' LIMIT 1";

3

Since you do not know how it is being stored, use the COLLATIONS CASE INSENSITIVE to carry out this work:

$sql = "SELECT * FROM users WHERE username COLLATE utf8_general_ci = '$Name' && userpass = '$PassMD5' LIMIT 1";

For accentuation is also used the latin1_general_ci.

Here is the list of collations supported by MariaDB.

1


The simplest way would be convert the strings before comparison:

$result1 = "SELECT * FROM users WHERE UPPER(username) = UPPER('$Name') && userpass = '$PassMD5' LIMIT 1";
  • If I were to use this conversation (for the letters to be uppercase), in the database would have to this "USER01"?

  • When comparing the two (both the database data and the login of the user who wants to log in) must be uppercase. In the answer I put the two (UPPER(username) = UPPER('$Name')). NOTE: using the function the letters are only uppercase in the comparison, not changed in the base.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.