Friday, June 8, 2007

Random Password Generator

PHP Script to Generate Random Password
Here is the script to generate a random 8 character length password. You can change the length to suit your needs.
/************Code*************************************/
/*** This fucnction takes one argument i.e. "abcdef...xyz123..90" and
* returns a random character
**
* $length = strlen($string);
* $postion=mt_rand(0,($length -1));
* //print("Rand_Character Function:($string[$postion])
");
* return ($string[$postion]);
*

** @param string $string
* @return Random Character
*/
function random_char($string)
{
$length = strlen($string);
$postion=mt_rand(0,($length -1));
return ($string[$postion]);
}
/*** This function takes two arguments the Characters String (e.g. "abcdef...xyz123..90")
* and length of the string (Password) to be created.
** @param string $charset_string
* @param integer $length
* @return string password
*/
function random_string($charset_string, $length)
{
$return_string = "";
for($i=0;$i<$length;$i++)
{
$return_string = $return_string.random_char($charset_string);
}
return ($return_string);
}
mt_srand((double)microtime()*1000000);
$charset = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$password_string =random_string($charset,8);
//Change the number 8 to the length of password you want to.
?>

No comments: