thinkphp 后台登陆密码加密传入密钥

10495

后台登陆密码加密验证设置:

相关函数:

/**
 * 对用户的密码进行加密
 * @param $password
 * @param $encrypt //传入加密串,在修改密码时做认证
 * @return array/password
 */
function get_password($password, $encrypt='') {
    $pwd = array();
    $pwd['encrypt'] =  $encrypt ? $encrypt : get_randomstr();
    $pwd['password'] = md5(md5(trim($password)).$pwd['encrypt']);
    return $encrypt ? $pwd['password'] : $pwd;
}
/**
 * 生成随机字符串
 * @param string $lenth 长度
 * @return string 字符串
 */
function get_randomstr($lenth = 6) {
    return get_random($lenth, '123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ');
}
/**
* 产生随机字符串
*
* @param    int        $length  输出长度
* @param    string     $chars   可选的 ,默认为 0123456789
* @return   string     字符串
*/
function get_random($length, $chars = '0123456789') {
    $hash = '';
    $max = strlen($chars) - 1;
    for($i = 0; $i < $length; $i++) {
        $hash .= $chars[mt_rand(0, $max)];
    }
    return $hash;
}

控制器方法:

/**
*$password 表单 提交过来的数据
*$user['encrypt'] 数据库数据
*/

if (!$user || ($user['password'] != get_password($password, $user['encrypt']))) {
$this->error('账号或密码错误');
}

玄玄博客:http://www.blogxuan.com/