PHP判断密码强度的方法详解
作者:武燕铭 时间:2023-06-14 03:00:08
本文实例讲述了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>
标签:PHP,密码强度
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python标准库06之子进程 (subprocess包) 详解
2021-05-24 02:00:25
MYSQL中 char 和 varchar的区别
2024-01-25 22:22:52
Python实现ping指定IP的示例
2023-10-05 04:20:10
![](https://img.aspxhome.com/file/2023/7/107187_0s.jpg)
python实现用于测试网站访问速率的方法
2023-07-28 19:12:02
YOLOv5目标检测之anchor设定
2022-04-23 16:22:10
![](https://img.aspxhome.com/file/2023/9/87809_0s.jpg)
MySQL转义字符的实际应用
2010-08-31 14:55:00
python3 批量获取对应端口服务的实例
2021-07-14 11:52:05
mysql远程跨库联合查询的示例
2024-01-13 23:09:10
![](https://img.aspxhome.com/file/2023/8/71098_0s.jpg)
Python内置函数之filter map reduce介绍
2023-01-18 21:07:16
Git 的基本操作、开发流程、实用技巧总结(陈彦贝)
2022-05-10 03:03:35
![](https://img.aspxhome.com/file/2023/4/120514_0s.jpg)
MySQL慢查询日志超详细总结
2024-01-17 00:17:21
微信小程序 ES6Promise.all批量上传文件实现代码
2024-02-26 15:32:10
MySQL8设置自动创建时间和自动更新时间的实现方法
2024-01-17 08:02:44
详解OpenCV和PIL读取和显示图像的差异
2021-07-06 22:17:22
![](https://img.aspxhome.com/file/2023/8/133288_0s.jpg)
发一个数字拼图网页游戏
2008-10-12 10:02:00
在PHP中读取和写入WORD文档的代码
2023-09-28 02:30:22
python3 通过 pybind11 使用Eigen加速代码的步骤详解
2023-05-13 21:53:18
![](https://img.aspxhome.com/file/2023/7/80497_0s.png)
python geemap的安装步骤及环境配置
2023-05-13 18:07:35
![](https://img.aspxhome.com/file/2023/1/112511_0s.jpg)
解决Django部署设置Debug=False时xadmin后台管理系统样式丢失
2022-05-09 00:33:57
![](https://img.aspxhome.com/file/2023/5/133225_0s.jpg)
浅谈js对象属性 通过点(.) 和方括号([]) 的不同之处
2024-04-22 13:01:00