php 检测密码,php 密码强度检测代码
在php編程中,尤其是用戶注冊(cè)這樣的模塊時(shí),對(duì)用戶密碼強(qiáng)度的要求,再多也不過(guò)。
現(xiàn)在很多網(wǎng)站,都會(huì)對(duì)密碼強(qiáng)度進(jìn)行檢測(cè),以獲取符合安全要求的用戶密碼。
不過(guò),有些對(duì)密碼強(qiáng)度檢測(cè)的功能,是建立在js或其它腳本上的,這可能會(huì)被惡意破壞者越過(guò)密碼檢測(cè),而進(jìn)行破壞。
本文介紹的這段代碼,基于對(duì)長(zhǎng)度、特殊字符、數(shù)字、字母等進(jìn)行檢測(cè),另外,還可以自己添加一些額外的字符,以加強(qiáng)密碼的安全性。
看代碼吧,如下:
/**
*
* @檢測(cè)密碼強(qiáng)度
* @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;
}
/*** 調(diào)用示例 ***/
$password = 'php_tutorials_and_examples!123';
echo testPassword($password);
?>
總結(jié)
以上是生活随笔為你收集整理的php 检测密码,php 密码强度检测代码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iis8.5 php mysql_Win
- 下一篇: php基础面试选择题,php面试之jav