PHP Password Generator

8th March 2008 - 1 minute read time

Here is a very simple function that will generate a string of random characters, ideal if you want to create a password for a new user.

  1. function generatePassword($length=10)
  2. {
  3. $pass = '';
  4. $parts = array_merge(range(0, 9),range('a', 'z'),range('A', 'Z'));
  5. for ($i=0; strlen($pass) <= $length; $i++) {
  6. $pass .= $parts[array_rand($parts)];
  7. }
  8. return $pass;
  9. }

Use the function like this.

echo generatePassword();

To create a longer password string just pass a parameter with the function.

echo generatePassword(5);




I have generated a very similar Password generator. It works really well and get quite a few hits towards it as well.

Password Generator (Thu, 06/02/2011 - 10:46)

Add new comment

The content of this field is kept private and will not be shown publicly.