Home >Backend Development >PHP Tutorial >php generates passwords of specified length and strength

php generates passwords of specified length and strength

WBOY
WBOYOriginal
2016-07-25 08:56:471144browse
This article introduces an example code that uses PHP to generate a password of specified length and strength. Friends in need can refer to it.

The following code can generate user passwords of specified length, and can also set the password strength, including passwords composed of lowercase letters, uppercase and lowercase letters, uppercase and lowercase letters and numeric passwords, uppercase and lowercase letters and numbers, special symbols, etc.

Code:

<?php
/**
* 函数:generateRandpassword
* 参数:$size 密码长度 $power密码强度 
* 功能:生成密码的函数
* 编辑:bbs.it-home.org
*/
function generateRandpassword($size=9, $power=0) {
    $vowels = 'aeuy';
    $randconstant = 'bdghjmnpqrstvz';
    if ($power & 1) {
        $randconstant .= 'BDGHJLMNPQRSTVWXZ';
    }
    if ($power & 2) {
        $vowels .= "AEUY";
    }
    if ($power & 4) {
        $randconstant .= '23456789';
    }
    if ($power & 8) {
        $randconstant .= '@#$%';
    }

    $Randpassword = '';
    $alt = time() % 2;
    for ($i = 0; $i < $size; $i++) {
        if ($alt == 1) {
            $Randpassword .= $randconstant[(rand() % strlen($randconstant))];
            $alt = 0;
        } else {
            $Randpassword .= $vowels[(rand() % strlen($vowels))];
            $alt = 1;
        }
    }
    return $Randpassword;
}
?>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn