<?
/*
Random password generator PHP script, written by Henric Blomgren (henric@digital-bless.com), 18 Jan 2006.
You are free to use, modify but not redistribuite any and all parts this
script, copyright © 2006 Henric Blomgren. Please email comments & patches :)
Useage:
This script is to be used inside another php script to generate a random a-Z password.
You can easily modify the variable $a_to_z below to contain other characters as well.
Usage is simple; call the function like this:
random_password(10);
- The number 10 represents the length of the password you want generated.
If you want your random password inside a variable you would simply:
$password = random_password(10);
Or, if you want the password printed out where the function was called:
echo random_password(10);
If you want (for an example) a hypen in a specific position in your password, then you could:
$password = random_password(3)."-".random_password(2);
*/
function random_password($length) {
if(is_numeric($length) AND $length < 35 AND $length > 1) {
$i = 0;
$random_str = '';
$a_to_z = 'aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ';
$max = strlen($a_to_z)-1;
while($i != $length) {
$rand = mt_rand(0, $max);
$random_str .= $a_to_z[$rand];
$i++;
}
return $random_str;
} else {
return 'Error, password can be max 35 chars.';
}
}
?>