使用 PHP 强制执行强密码

发布于 2024-12-01 23:35:39 字数 419 浏览 0 评论 0原文

我正在尝试检查用户是否创建了包含符号(包括+、-、= 符号)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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

韶华倾负 2024-12-08 23:35:39

我对密码强度检查的建议是将每个要求分解为单独的正则表达式,特别是:

   $regexs = array(
                 '/[0-9]+/',  // Numbers
                 '/[a-z]+/',  // Lower Case Letters
                 '/[A-Z]+/',  // Upper Case Letters
                 '/[+-=]+/',  // Your list of allowable symbols.
   );

将计数器初始化为 0。对于每个正则表达式,测试它是否匹配。如果是,则将计数器增加 1。如果计数器大于 3(如果您希望他们拥有真正强的密码,则返回 4),则返回 true,否则返回 false。

我认为,从长远来看,如果您的需求发生变化,这对您来说更容易维护。

My suggestion for password strength checking is to break each requirement you have into separate regexs, particularly:

   $regexs = array(
                 '/[0-9]+/',  // Numbers
                 '/[a-z]+/',  // Lower Case Letters
                 '/[A-Z]+/',  // Upper Case Letters
                 '/[+-=]+/',  // Your list of allowable symbols.
   );

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.

素年丶 2024-12-08 23:35:39

您不必列出所有这些字符。任何不是数字或字母的东西都是特殊字符(或空格):

(?:[^0-9A-z]|[0-9])

You don't have to list all those characters. Anything that is not a number or a letter is a special character (or space):

(?:[^0-9A-z]|[0-9])
朮生 2024-12-08 23:35:39

我建议从不同的方向寻找。如果要施加限制,只需要求更长即可。正如已经指出的,使用符号很可能会导致用户忘记密码并且永远不会回到您的网站。为了获得最大的安全性,请在注册页面上发布以下漫画: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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文