使用 PHP 强制执行强密码
我正在尝试检查用户是否创建了包含符号(包括+、-、= 符号)OR/AND 数字的密码。 我怎样才能做到这一点?
function check_password($str)
{
if (!preg_match ("/[&@<>%\*\,\^!#$%().]/i", $str))
{
$this->form_validation->set_message('check_password', 'Your password should contain a number,letter,and special characters"');
return FALSE;
}
else
{
return TRUE;
}
}
谢谢。
I am trying to check, if a user has created a password that contains symbols (including +,-,= signs) OR/AND numbers.
How can I do that?
function check_password($str)
{
if (!preg_match ("/[&@<>%\*\,\^!#$%().]/i", $str))
{
$this->form_validation->set_message('check_password', 'Your password should contain a number,letter,and special characters"');
return FALSE;
}
else
{
return TRUE;
}
}
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我对密码强度检查的建议是将每个要求分解为单独的正则表达式,特别是:
将计数器初始化为 0。对于每个正则表达式,测试它是否匹配。如果是,则将计数器增加 1。如果计数器大于 3(如果您希望他们拥有真正强的密码,则返回 4),则返回 true,否则返回 false。
我认为,从长远来看,如果您的需求发生变化,这对您来说更容易维护。
My suggestion for password strength checking is to break each requirement you have into separate regexs, particularly:
Initialize a counter to 0. For each regex, test to see if it matches. If it does, increase the counter by 1. Then return true if counter is greater than 3 (or 4 if you want them to have a really really strong password), otherwise return false.
I think this would be much more maintainable for you in the long run if your requirements ever change.
您不必列出所有这些字符。任何不是数字或字母的东西都是特殊字符(或空格):
You don't have to list all those characters. Anything that is not a number or a letter is a special character (or space):
我建议从不同的方向寻找。如果要施加限制,只需要求更长即可。正如已经指出的,使用符号很可能会导致用户忘记密码并且永远不会回到您的网站。为了获得最大的安全性,请在注册页面上发布以下漫画:http://xkcd.com/936/
I recommend looking in a different direction. If you are to impose a restriction, just require it to be longer. As has been pointed out, using symbols will most likely cause the user to forget the password and never come back to your site. For maximum security post the following comic on the registration page: http://xkcd.com/936/