This topic is locked

How to allow case insensitive login

9/30/2011 10:06:22 AM
PHPRunner Tips and Tricks
F
FunkDaddy author

I wish PHPR gave us an option to simply check off whether we want the login variables to be enforced in a case sensitive basis or not (ex: place a check box in the miscellaneous tab for username and one for password).
Since they don't have that option here's what you need to add to your "Before Login" event in your login form:



//Call up the variable present in the login.php file

global $strSQL, $cUserNameField, $cPasswordField, $sUsername, $sPassword, $conn, $logged;
//fetch user records

$rs=db_query($strSQL,$conn);

$data=db_fetch_array($rs);
//If username is found in db then compare them to login input using lower case for comparison

//Only username is case insensitive here... we leave password alone since we want it to remain case-sensitive

if($data)

if(@strtolower($data[$cUserNameField]) == strtolower(trim($username)) && @$data[$cPasswordField]==$sPassword)

$logged=true;
$sUsername = $username;//declare this to pass back to login.php file


Done.

F
FunkDaddy author 9/30/2011

Quick issue to point out...
I am using MySQL with _ci collation which makes queries case insensitive by default. Meaning that if your db doesn't have this feature it will never even populate your $data array with the $cUserNameField to begin with, thus causing your login to fail. Make sure if you run into problem to check whether your db is setup to query without case sensitivity. Otherwise you can try the approach that sergey provided instead as follows (note, his approach did not work for me... not sure why):



global $strSQL, $cUserNameField, $cPasswordField, $sUsername, $sPassword;
$username = strtolower(trim($username));

$password = strtolower(trim($password));
$strSQL = "select * from ".AddTableWrappers("userstable")." where ".AddFieldWrappers($cUserNameField).

"='".$username."' and ".AddFieldWrappers($cPasswordField).

"='".$password."'";
$sUsername = $username;

$sPassword = $password;


Good luck.