How to generate random password with PHP?

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

Or is there a software to auto generate random passwords?

相关标签:
19条回答
  • 2020-12-01 01:29

    Just build a string of random a-z, A-Z, 0-9 (or whatever you want) up to the desired length. Here's an example in PHP:

    function generatePassword($length = 8) {
        $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $count = mb_strlen($chars);
    
        for ($i = 0, $result = ''; $i < $length; $i++) {
            $index = rand(0, $count - 1);
            $result .= mb_substr($chars, $index, 1);
        }
    
        return $result;
    }
    

    To optimize, you can define $chars as a static variable or constant in the method (or parent class) if you'll be calling this function many times during a single execution.

    0 讨论(0)
  • 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;   
    }
    
    0 讨论(0)
  • 2020-12-01 01:30

    You could just use PEAR's Text_Password package - it supports quite a few algorithms for generating them; and frees up your time to go onto something else.

    0 讨论(0)
  • 2020-12-01 01:35
    function generate_password($length = 6) {
        $password = '';
        $chars = array_merge(range('a', 'z'), range('A', 'Z'), range(0, 9));
        for ($i = 0; $i < $length; $i ++) {
            $password .= $chars[array_rand($chars)];
        }
        return $password;
    }
    
    0 讨论(0)
  • 2020-12-01 01:36

    This function will include half digits and half numbers:

         function generateMixedPassword($length = 8) {
            $base = 'abcdefghijklmnopqrstuvwxyz';
            $baseD = '0123456789';
    
            $r = array();
    
            for ($i = 0; $i < $length; $i += 2) {
                $r[] = substr($base, rand(0, strlen($base) - 1), 1);
            }
            for ($i = 0; $i < $length; $i += 2) {
                $r[] = substr($baseD, rand(0, strlen($baseD) - 1), 1);
            }
            shuffle($r);
    
            return implode('', $r);
        }
    

    Using the same login, it can be extended to also include special characters

    0 讨论(0)
  • 2020-12-01 01:38

    Pwgen-php produces save, pronounceable (easier to remember) passwords: http://code.google.com/p/pwgen-php/

    Might that be acceptable?

    0 讨论(0)
提交回复
热议问题