回答の編集:
修正は次のように変更することでした
if (get_user_data( $input_user, $logindata ) === $input_pwd ) {
:
if (get_user_data(strtolower($input_user), $logindata) === $input_pwd ) {
ユーザー名が強制的に小文字になるようにします。ユーザー名もすべて小文字として保存することを意識する必要があります。
私は知っていstrcasecmp
ます。2つの変数しか比較できないため、それが私の作業コードにどのように適用されるかはわかりません。
preg_match
以下の作業コードのコンテキストで大文字と小文字を区別しないようにすることはできますか?返された変数/i
のコマンドに正規表現を追加できますか?preg_match
ユーザーが入力したユーザー名(ドメイン名を含む)を大文字と小文字を区別しないようにしたいだけです。(つまり、uSeRnAMe @ dOmAIN1.CoM)有効なユーザー名のすべての組み合わせを疑似データベースに追加する必要はありません!
これは私の作業コードです:
// Get users
$input_pwd = ( isset( $_POST["password"] ) ? $_POST["password"] : '' );
$input_user = ( isset( $_POST["username"] ) ? $_POST["username"] : '' );
// Your pseudo database here
$usernames = array(
"username@domain1.com",
"username2@domain1.com",
"username3@domain1.com",
"username1@domain2.com",
"/[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(
"username@domain1.com" => "http://www.google.com",
"username2@domain1.com" => "http://www.yahoo.com",
"username3@domain1.com" => "http://www.stackoverflow.com",
"username1@domain2.com" => "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;
}