温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用PHP怎么对密码的强度进行判断

发布时间:2021-01-27 17:15:54 来源:亿速云 阅读:129 作者:Leah 栏目:开发技术

使用PHP怎么对密码的强度进行判断?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

一、php页面

$score = 0;
if(!empty($_GET['value'])){ //接收的值
    $str = $_GET['value'];
} else{
    $str = '';
}
if(preg_match("/[0-9]+/",$str))
{
    $score ++;
}
if(preg_match("/[0-9]{3,}/",$str))
{
    $score ++;
}
if(preg_match("/[a-z]+/",$str))
{
    $score ++;
}
if(preg_match("/[a-z]{3,}/",$str))
{
    $score ++;
}
if(preg_match("/[A-Z]+/",$str))
{
    $score ++;
}
if(preg_match("/[A-Z]{3,}/",$str))
{
    $score ++;
}
if(preg_match("/[_|\-|+|=|*|!|@|#|$|%|^|&|(|)]+/",$str))
{
    $score += 2;
}
if(preg_match("/[_|\-|+|=|*|!|@|#|$|%|^|&|(|)]{3,}/",$str))
{
    $score ++ ;
}
if(strlen($str) >= 10)
{
    $score ++;
}
echo $score;
exit;

二、html页面

<table cellspacing="0" cellpadding="0">
<tr>
<td>输入密码:</td>
<td colspan="4"><input type="password" value="" name="newpwd" onblur="getPassword();" />
</tr>
<tr>
<td>密码强度:</td>
<td id="idSM1" align="middle" width="20%"><span >&nbsp;</span><span id="idSMT1" >弱</span></td>
<td id="idSM2"  align="middle" width="20%"><span >&nbsp;</span><span id="idSMT0" >无</span><span id="idSMT2" >中等</span></td>
<td id="idSM3"  align="middle" width="20%"><span >&nbsp;</span><span id="idSMT3" >强</span></td>
<td id="idSM4"  align="middle" width="20%"> <span >&nbsp;</span><span id="idSMT4" >极好</span></td>
</tr>
</table>

三、js

<script>
function getPassword(){
    var value = $("input[name='newpwd']").attr('value');
    $.get('index.php?r=account/testpwd',{value:value},function(data){
        if(data>=1 && data<=3){
            $('#idSM1').attr('class','pwdChkCon1'); //弱
            $('#idSM2').attr('class','pwdChkCon0');
            $('#idSM3').attr('class','pwdChkCon0');
            $('#idSM4').attr('class','pwdChkCon0');
            $('#idSMT1').show();
            $('#idSMT0').hide();
            $('#idSMT2').hide();
            $('#idSMT3').hide();
            $('#idSMT4').hide();
        } else if(data>=4 && data<=6){ //中等
            $('#idSM1').attr('class','pwdChkCon2');
            $('#idSM2').attr('class','pwdChkCon2');
            $('#idSM3').attr('class','pwdChkCon0');
            $('#idSM4').attr('class','pwdChkCon0');
            $('#idSMT0').hide();
            $('#idSMT1').hide();
            $('#idSMT2').show();
            $('#idSMT3').hide();
            $('#idSMT4').hide();
        } else if(data>=7 && data<=8){ //强
            $('#idSM1').attr('class','pwdChkCon3');
            $('#idSM2').attr('class','pwdChkCon3');
            $('#idSM3').attr('class','pwdChkCon3');
            $('#idSM4').attr('class','pwdChkCon0');
            $('#idSMT0').hide();
            $('#idSMT1').hide();
            $('#idSMT2').hide();
            $('#idSMT3').show();
            $('#idSMT4').hide();
        } else if(data>=9 && data<=10){ //极好
            $('#idSM1').attr('class','pwdChkCon4');
            $('#idSM2').attr('class','pwdChkCon4');
            $('#idSM3').attr('class','pwdChkCon4');
            $('#idSM4').attr('class','pwdChkCon4');
            $('#idSMT0').hide();
            $('#idSMT1').hide();
            $('#idSMT2').hide();
            $('#idSMT3').hide();
            $('#idSMT4').show();
        }
    });
}

四、css

<style>
.pwdChkCon0 {BORDER-RIGHT: #bebebe 1px solid;BORDER-BOTTOM: #bebebe 1px solid;BACKGROUND-COLOR: #ebebeb;TEXT-ALIGN: center;}
.pwdChkCon1 {BORDER-RIGHT: #bb2b2b 1px solid;BORDER-BOTTOM: #bb2b2b 1px solid;BACKGROUND-COLOR: #ff4545;TEXT-ALIGN: center;}
.pwdChkCon2 {BORDER-RIGHT: #e9ae10 1px solid;BORDER-BOTTOM: #e9ae10 1px solid;BACKGROUND-COLOR: #ffd35e;TEXT-ALIGN: center;}
.pwdChkCon3 {BORDER-RIGHT: #267a12 1px solid;BORDER-BOTTOM: #267a12 1px solid;BACKGROUND-COLOR: #3abb1c;TEXT-ALIGN: center;}
.pwdChkCon4 {BORDER-RIGHT: #267a12 1px solid;BORDER-BOTTOM: #267a12 1px solid;BACKGROUND-COLOR: #3abb1c;TEXT-ALIGN: center;}
</style>

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php
AI