答案编辑:PHP不区分大小写usermame比赛
的修复是改变: 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
命令中以返回变量吗?
我只想让用户输入的用户名(包括域名)不区分大小写。 (即[email protected]),而无需将有效用户名的每个组合添加到我的伪数据库!
这是我的工作代码:
// 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
"/[^@][email protected]\.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",
"/[^@][email protected]\.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;
}
我不知道你的意思是“返回的变量”,但将我加入preg_match会使其不区分大小写。 – RonaldBarzell
你可以在代码片段的上下文中添加你的回复,我可以接受你的答案:) – James
只是做了:)。我希望这段代码足够了。 – RonaldBarzell