Generate Random String/Password
Posted by Pete | Posted in PHP Functions | Posted on 16-07-2009-05-2008
2
Having a secure password is important while surfing the internet. So generating a completly random password is a good way to stay secure. So with this fully commented function it will be simple to implement.
// Generate Random String (Default: 8 Characters) function generaterand($length = 8) { // start with a blank password $password = ""; // define possible characters $possible = "0123456789bcdfghjkmnpqrstvwxyz"; // set up a counter $i = 0; // add random characters to $password until $length is reached while ($i < $length) { // pick a random character from the possible ones $char = substr($possible, mt_rand(0, strlen($possible) - 1), 1); // we don't want this character if it's already in the password if (!strstr($password, $char)) { $return .= $char; $i++; } } // done! return $return; }
To execute the function is simple
<?php $pass = generaterand(); echo "Password" . $pass; ?>
If you need any help with this function or like my tutorials/scripts please leave a comment.



Thanks for great tutorial
. But i want to ask, if function is “generaterand” why $pw and $pw2 variables values is “generate” not “generaterand”.
Ooops thats a spelling mistake. Had been a long day when i was writing this