首页 > 后端开发 > php教程 > PHP判断密码强度的方法

PHP判断密码强度的方法

墨辰丷
发布: 2023-03-27 11:18:01
原创
2324 人浏览过

这篇文章主要介绍了PHP判断密码强度的方法,涉及php正则判断、ajax交互及页面元素动态操作相关实现技巧,需要的朋友可以参考下

具体如下:

一、php页面

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

$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页面

1

2

3

4

5

6

7

8

9

10

11

12

13

<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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

<script>

function getPassword(){

    var value = $("input[name=&#39;newpwd&#39;]").attr(&#39;value&#39;);

    $.get(&#39;index.php?r=account/testpwd&#39;,{value:value},function(data){

        if(data>=1 && data<=3){

            $(&#39;#idSM1&#39;).attr(&#39;class&#39;,&#39;pwdChkCon1&#39;); //弱

            $(&#39;#idSM2&#39;).attr(&#39;class&#39;,&#39;pwdChkCon0&#39;);

            $(&#39;#idSM3&#39;).attr(&#39;class&#39;,&#39;pwdChkCon0&#39;);

            $(&#39;#idSM4&#39;).attr(&#39;class&#39;,&#39;pwdChkCon0&#39;);

            $(&#39;#idSMT1&#39;).show();

            $(&#39;#idSMT0&#39;).hide();

            $(&#39;#idSMT2&#39;).hide();

            $(&#39;#idSMT3&#39;).hide();

            $(&#39;#idSMT4&#39;).hide();

        } else if(data>=4 && data<=6){ //中等

            $(&#39;#idSM1&#39;).attr(&#39;class&#39;,&#39;pwdChkCon2&#39;);

            $(&#39;#idSM2&#39;).attr(&#39;class&#39;,&#39;pwdChkCon2&#39;);

            $(&#39;#idSM3&#39;).attr(&#39;class&#39;,&#39;pwdChkCon0&#39;);

            $(&#39;#idSM4&#39;).attr(&#39;class&#39;,&#39;pwdChkCon0&#39;);

            $(&#39;#idSMT0&#39;).hide();

            $(&#39;#idSMT1&#39;).hide();

            $(&#39;#idSMT2&#39;).show();

            $(&#39;#idSMT3&#39;).hide();

            $(&#39;#idSMT4&#39;).hide();

        } else if(data>=7 && data<=8){ //强

            $(&#39;#idSM1&#39;).attr(&#39;class&#39;,&#39;pwdChkCon3&#39;);

            $(&#39;#idSM2&#39;).attr(&#39;class&#39;,&#39;pwdChkCon3&#39;);

            $(&#39;#idSM3&#39;).attr(&#39;class&#39;,&#39;pwdChkCon3&#39;);

            $(&#39;#idSM4&#39;).attr(&#39;class&#39;,&#39;pwdChkCon0&#39;);

            $(&#39;#idSMT0&#39;).hide();

            $(&#39;#idSMT1&#39;).hide();

            $(&#39;#idSMT2&#39;).hide();

            $(&#39;#idSMT3&#39;).show();

            $(&#39;#idSMT4&#39;).hide();

        } else if(data>=9 && data<=10){ //极好

            $(&#39;#idSM1&#39;).attr(&#39;class&#39;,&#39;pwdChkCon4&#39;);

            $(&#39;#idSM2&#39;).attr(&#39;class&#39;,&#39;pwdChkCon4&#39;);

            $(&#39;#idSM3&#39;).attr(&#39;class&#39;,&#39;pwdChkCon4&#39;);

            $(&#39;#idSM4&#39;).attr(&#39;class&#39;,&#39;pwdChkCon4&#39;);

            $(&#39;#idSMT0&#39;).hide();

            $(&#39;#idSMT1&#39;).hide();

            $(&#39;#idSMT2&#39;).hide();

            $(&#39;#idSMT3&#39;).hide();

            $(&#39;#idSMT4&#39;).show();

        }

    });

}

登录后复制

四、css

1

2

3

4

5

6

7

<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>

登录后复制

相关推荐:

js使用正则进行密码强度验证

JS的密码强度校验正则表达式(附代码)

验证用户设置的密码强度正则表达式

以上是PHP判断密码强度的方法的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板