How to generate random password with PHP?

后端 未结 19 2205
栀梦
栀梦 2020-12-01 00:45

Or is there a software to auto generate random passwords?

19条回答
  •  爱一瞬间的悲伤
    2020-12-01 01:29

    here is a function that generates a password with a minimum length, a minimum number of digits and a minimal number of letters.

    function generatePassword() {
    $min_length=8;  //Minimum length of the password
    $min_numbers=2; //Minimum of numbers AND special characters
    $min_letters=2; //Minimum of letters
    
    $password = '';
    $numbers=0;
    $letters=0;
    $length=0;
    
    while ( $length <= $min_length OR $numbers <= $min_numbers OR $letters <= $min_letters) {
        $length+=1;
        $type=rand(1, 3);
        if ($type==1) {
            $password .= chr(rand(33, 64)); //Numbers and special characters
            $numbers+=1;
        }elseif ($type==2) {
            $password .= chr(rand(65, 90)); //A->Z
            $letters+=1;
        }else {
            $password .= chr(rand(97, 122)); //a->z
            $letters+=1;
        }
    
    }
    return $password;   
    }
    

提交回复
热议问题