Reputation: 5168
In my website usernames are saved like this robert and funny thing is that one can registers with the name Robert (Capital R).
How would I prevent these somehow duplicated usernames?
My project is in mysql/php
if (mysql_num_rows(mysql_query("SELECT username FROM user_table WHERE username='$username'")) > 0){
die("duplicated usernames can't be saved , this username exists.");
}
Even with this code Robert can be registered.
Upvotes: 1
Views: 620
Reputation: 8186
php has very important function but people ignore it in start strtolower
, use it at the time of
registering and query hence
$username = strtolower($username);
you will never face such problem again .
Upvotes: 0
Reputation: 598
Hopefully you've sanitized the user input for the username to prevent SQL injections, after that, use PHP to LC your usernames before checking for duplicates or inserting the name in the database:
$username = strtolower($username);
if (mysql_num_rows(mysql_query("SELECT username FROM user_table WHERE username='$username'")) > 0){
die("duplicated usernames can't be saved , this username exists.");
}
Upvotes: 0
Reputation: 12608
Convert them both to the same case and then compare them. As such:
SELECT username FROM table WHERE LOWER(username) = LOWER('$username')
Upvotes: 2
Reputation: 8035
You should add a UNIQUE index on the username column.
Also, you may find some useful info here: https://stackoverflow.com/search?q=mysql+case+sensitive
Upvotes: 3