Question

Im using the following code to check if username exists, But this script ignores capital letters.

So JAMES is the same as James

Q1:

How do i ensure that the string matches the existing strings(UserNames) in the database as they are and JAMES is not equated to James.

Q2: mysql_real_escape_string

I only want to allow Numbers and Letters, but i want to detect these unwanted CHARS and tell the user that those funny CHARS are not allowed.

Here's my PHP Block of Code

$l = mysql_real_escape_string($_POST['l']);
     if ( $l == ''){
    echo 'Please Enter your desired username.';
} else {
$Result = mysql_query("SELECT * FROM users WHERE uname = '$l'") or die (mysql_error()); 
if (mysql_num_rows($Result) > 0){
     echo '<span style="color:#F00">Sorry this User-Name is already taken, please choose another User-Name</span>';
} else {
    echo '<span style="color:#090">'.$l.' is ok, Please go ahead and fill the form.</span>';
    }
}

NOTES: - I'm aware of my deprecated mysql, i'll sort it out soon.

Was it helpful?

Solution

Q1 - eh? Dunno why you would want to do this but here you go anyway :-D

"SELECT * FROM users WHERE BINARY `uname` = '$l'"

That makes your search case sensitive

Q2 - simplest way is to just regex is

$newString = preg_replace("/[^a-zA-Z0-9]+/", "", $oldStringWithLoadsOfNaughtyStuff);

if($newString !== $oldStringWithLoadsOfNaughtyStuff){
    //naughty characters must be there as strings do not match.
    echo "naughty Naughty - you have enter illegal characters - only letters and numbers please";

}else{

    //all good here

}

OTHER TIPS

Binary collation can be used:

$Result = mysql_query("SELECT * FROM users WHERE uname = '$l' COLLATE utf8_bin;");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top