Lightweight password generator
Here’s a snippet of PHP code that can create passwords. It can create passwords of different lengths, it can use different sets (uppercase and lowercase letters as well as numbers), and it can be told to exclude potentially confusing characters.
<?php function passgen($length=8, $reqsets='uln', $noconfusing='true') { $length = (int)$length; if ($length > 20 || $length < 3) $length = 8; // some characters look alike. let's optionally exclude them. $confusing = ($noconfusing === 'true') ? 'O01Il' : ''; $randchars = $token = ''; $sets = array( 'u' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'l' => 'abcdefghijklmnopqrstuvwxyz', 'n' => '0123456789' // you can even add an own set ); if ($reqsets) { $reqsets = str_split($reqsets); foreach ( array_keys($sets) as $availset ) if ( in_array($availset, $reqsets) ) $randchars .= $sets[$availset]; } if (!$randchars) $randchars = implode($sets); for ($i=0; $i<=$length; $i++) { do { $lpos = mt_rand( 0, strlen($randchars)-1 ); $letter = substr($randchars, $lpos, 1); } while ( in_array($letter, str_split($confusing) ) ); $token .= $letter; } return $token; } echo passgen($_GET['length'], $_GET['sets'], $_GET['noconfusing']); ?>
If you intend to use this in a script, please keep in mind that mt_rand() needs to be seeded/initialized properly.

