php 检测密码,php 密码强度检测代码

在php编程中,尤其是用户注册这样的模块时,对用户密码强度的要求,再多也不过。

现在很多网站,都会对密码强度进行检测,以获取符合安全要求的用户密码。

不过,有些对密码强度检测的功能,是建立在js或其它脚本上的,这可能会被恶意破坏者越过密码检测,而进行破坏。

本文介绍的这段代码,基于对长度、特殊字符、数字、字母等进行检测,另外,还可以自己添加一些额外的字符,以加强密码的安全性。

看代码吧,如下:

/**

*

* @检测密码强度

* @param string $password

* @return int

* @edit www.jbxue.com

*/

function testPassword($password)

{

if ( strlen( $password ) == 0 )

{

return 1;

}

$strength = 0;

/*** get the length of the password ***/

$length = strlen($password);

/*** check if password is not all lower case ***/

if(strtolower($password) != $password)

{

$strength += 1;

}

/*** check if password is not all upper case ***/

if(strtoupper($password) == $password)

{

$strength += 1;

}

/*** check string length is 8 -15 chars ***/

if($length >= 8 && $length <= 15)

{

$strength += 1;

}

/*** check if lenth is 16 - 35 chars ***/

if($length >= 16 && $length <=35)

{

$strength += 2;

}

/*** check if length greater than 35 chars ***/

if($length > 35)

{

$strength += 3;

}

/*** get the numbers in the password ***/

preg_match_all('/[0-9]/', $password, $numbers);

$strength += count($numbers[0]);

/*** check for special chars ***/

preg_match_all('/[|!@#$%&*\/=?,;.:\-_+~^\\\]/', $password, $specialchars);

$strength += sizeof($specialchars[0]);

/*** get the number of unique chars ***/

$chars = str_split($password);

$num_unique_chars = sizeof( array_unique($chars) );

$strength += $num_unique_chars * 2;

/*** strength is a number 1-10; ***/

$strength = $strength > 99 ? 99 : $strength;

$strength = floor($strength / 10 + 1);

return $strength;

}

/*** 调用示例 ***/

$password = 'php_tutorials_and_examples!123';

echo testPassword($password);

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值