PHP判断密码强度的方法详解

PHP piniu 817浏览 0评论

在这里我通过以下实例的方式来展示PHP是如何判断密码强度的

1.php代码如下:

<?php

//接收的值
if (!empty($_GET['value'])) { 
    $str = $_GET['value'];
} else {
    $str = '';
}
//通过正则判断值的复杂度,得出相应的值
$score = 0;
if (preg_match("/[0-9F]+/", $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;

2.html页面的相关代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>密码强度判断</title>
</head>

<style type="text/css">
    .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>

<body>

<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 style="height:0px; line-height:0px;"> </span><span id="idSMT1" style="DISPLAY: none">弱</span></td>
        <td id="idSM2" style="BORDER-LEFT: #fff 1px solid" align="middle" width="20%"><span style="height:0px; line-height:0px;"> </span><span id="idSMT0" style="DISPLAY:inline; FONT-WEIGHT: normal; COLOR: #666">无</span><span id="idSMT2" style="DISPLAY: none">中等</span></td>
        <td id="idSM3" style="BORDER-LEFT: #fff 1px solid" align="middle" width="20%"><span style="height:0px; line-height:0px;"> </span><span id="idSMT3" style="DISPLAY: none">强</span></td>
        <td id="idSM4" style="BORDER-LEFT: #fff 1px solid" align="middle" width="20%"> <span style="height:0px; line-height:0px;"> </span><span id="idSMT4" style="DISPLAY: none">极好</span></td>
    </tr>
</table>

<script src="https://cdn.bootcss.com/jquery/1.11.1/jquery.js"></script>
<script type="text/javascript">
    function getPassword(){
        var value = $("input[name='newpwd']").val();
        $.get('password.php?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();
            }
        });
    }
</script>
</body>
</html>

发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • * 昵称:
  • * 邮箱: