答案编辑:
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;
}