Reputation: 17
ANSWER EDIT:
The fix was to change:
if (get_user_data( $input_user, $logindata ) === $input_pwd ) {
to
if (get_user_data(strtolower($input_user), $logindata) === $input_pwd ) {
so that the username is forced to lowercase. I just have to be conscious to store my usernames as all lowercase too.
I am aware of strcasecmp
. I am not sure how that would apply to my working code though, as you can only compare 2 variables.
Am I able to make preg_match
case insensitive in the context of my working code below?
Can I add the /i
regex to my preg_match
command to a returned variable?
I just want the username that is entered by the user (including domain name) to be case insenstive. (ie. [email protected]) without having to add every combination of valid username to my pseudo database!
This is my working code:
// Get users
$input_pwd = ( isset( $_POST["password"] ) ? $_POST["password"] : '' );
$input_user = ( isset( $_POST["username"] ) ? $_POST["username"] : '' );
// Your pseudo database here
$usernames = array(
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"/[a-z][A-Z][0-9]@domain2\.com/", // use an emtpy password string for each of these
"/[^@]+@domain3\.com/" // entries if they don't need to authenticate
);
$passwords = array( "password1", "password2", "password3", "password4", "", "" );
// Create an array of username literals or patterns and corresponding redirection targets
$targets = array(
"[email protected]" => "http://www.google.com",
"[email protected]" => "http://www.yahoo.com",
"[email protected]" => "http://www.stackoverflow.com",
"[email protected]" => "http://www.serverfault.com",
"/[a-z][A-Z][0-9]@domain2\.com/" => "http://target-for-aA1-usertypes.com",
"/[^@]+@domain3\.com/" => "http://target-for-all-domain3-users.com",
"/.+/" => "http://default-target-if-all-else-fails.com",
);
$logindata = array_combine( $usernames, $passwords );
if ( get_user_data( $input_user, $logindata ) === $input_pwd ) {
session_start();
$_SESSION["username"] = $input_user;
header('Location: ' . get_user_data( $input_user, $targets ) );
exit;
} else {
// Supplied username is invalid, or the corresponding password doesn't match
header('Location: login.php?login_error=1');
exit;
}
function get_user_data ( $user, array $data ) {
$retrieved = null;
foreach ( $data as $user_pattern => $value ) {
if (
( $user_pattern[0] == '/' and preg_match( $user_pattern, $user ) )
or ( $user_pattern[0] != '/' and $user_pattern === $user)
) {
$retrieved = $value;
break;
}
}
return $retrieved;
}
Upvotes: 0
Views: 1708
Reputation: 3830
You can do a case insensitive match in PHP with i. For instance, the following will print 'This matches!':
<?php
if ( preg_match('/def/i', 'ABCDEF') ) {
echo 'This matches!';
}
?>
So just add i to the pattern, and the case will be ignored.
Upvotes: 1
Reputation: 30575
One approach if you want case-insensitive usernames is to always lowercase a new one when you store it, and then to always lowercase the comparing value when you check. (This is a lot faster than using preg_match
.)
Upvotes: 0