PHP Password Generating

This script allows random passwords to be generated of any required length with any combination of characters.

Randomly generated password: m1ztpxw8

The code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
function genPwd($length=6) {
   $password = '';
   $possible = '23456789bcdfghjkmnpqrstvwxyz';
   $i = 0;
   while ($i < $length) {
 
      $password .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
      $i++;
   }
 
   return $password;
}
?>

Usage

Copy and paste the above function into your page and then call the genPwd() function.

1
2
3
<?php
   $password = genPwd(8);
?>

If no length parameter is set then a default length of 6 will be used. The $possible variable within the function code can be edit to allow or disallow certain characters.

Posted by admin on Friday, March 5th, 2010