background image

php 密码强度检测代码

    本文介绍下,用 php 实现的用于检测密码强度的一段代码,有需要的朋友参考下。

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

现在很多网站,都会对密码强度进行检测,以获取符合安全要求的用户密码。
不过,有些对密码强度检测的功能,是建立在

js 或其它脚本上的,这可能会被恶意破坏者

越过密码检测,而进行破坏。
   本文介绍的这段代码,基于对长度、特殊字符、数字、字母等进行检测,另外,还可以自己
添加一些额外的字符,以加强密码的安全性。
看代码吧,如下:
<?php
/**
 *
 * @检测密码强度
 * @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)