本文实例讲述了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 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>
三、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>
ps:这里再为大家提供几款相关在线工具供大家参考使用:
密码安全性在线检测:
http://tools.jb51.net/password/my_password_safe
在线随机数字/字符串生成工具:
http://tools.jb51.net/aideddesign/suijishu
高强度密码生成器:
http://tools.jb51.net/password/createstrongpassword
更多关于php相关内容感兴趣的读者可查看本站专题:《php加密方法总结》、《php编码与转码操作技巧汇总》、《php数学运算技巧总结》、《php数组(array)操作技巧大全》、《php字符串(string)用法总结》、《php数据结构与算法教程》、《php程序设计算法总结》、《php正则表达式用法总结》、及《php常见数据库操作技巧汇总》
希望本文所述对大家php程序设计有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!