PDA

View Full Version : Create random string


Mathminded
4-7-03, 04:21 PM
I need to create a random string of characters. I've seen how to do it before, but now I can't recall exactly what they did. I'd like to create a 10-character string. I'd also like to eliminate "confusing" characters like 0 and O, etc. Could someone please explain how to do this?

Thanks!

Mathminded
4-7-03, 04:44 PM
I found something at php.net. It seems to do just what I wanted.



function RandomPassword( $passwordLength ) {

$passwordChars = '23456789'
.'ABCDEFGHIJKLMNPQRSTUVWXYZ'
.'abcdefghijkmnpqrstuvwxyz';

$password = "";

for ($index = 1; $index <= $passwordLength; $index++) {

// pick random number
$randomNumber = rand(1,strlen($passwordChars));

$password .= substr($passwordChars,$randomNumber-1,1);

}

return $password;

}




Notice I removed 0, o, O, 1, and l from $passwordChars so my easily confused users don't get confused. :-)

netaustin
4-7-03, 08:14 PM
if i were one of your users, i wouldn't much like having a password assigned to me (i'd forget it and then never return)

i let them pick their own password, then i store it using PHP's crypt function (one way encryption) which lets me compare the password they entered to the password i have stored for their username.

Mathminded
4-9-03, 07:14 AM
I agree. I hate it when people issue a password to me. This isn't for passwords, though. It's for allowing 700 people to participate in an election via the internet. They'll each be provided with a slip of paper with the randomly generated string. That string will allow them to enter one vote in the election.

Pig
4-9-03, 08:02 AM
This system is also great for generating random passwords for INITIAL account set up where you want to require a valid email (have to check the email to get the password), or if you need to reset their password.