How to generate random password with PHP?

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

Or is there a software to auto generate random passwords?

19条回答
  •  广开言路
    2020-12-01 01:25

    Here's a simple solution. It will contain lowercase letters and numbers.

    substr(str_shuffle(strtolower(sha1(rand() . time() . "my salt string"))),0, $PASSWORD_LENGTH);
    

    Here's A stronger solution randomly generates the character codes in the desired character range for a random length within a desired range.

    function generateRandomPassword() {
      //Initialize the random password
      $password = '';
    
      //Initialize a random desired length
      $desired_length = rand(8, 12);
    
      for($length = 0; $length < $desired_length; $length++) {
        //Append a random ASCII character (including symbols)
        $password .= chr(rand(32, 126));
      }
    
      return $password;
    }
    

提交回复
热议问题