PHP - Generate a random password based on length

Here's a quick function to generate a password. All you have to do is call it passing the length of your required password as an argument.

<?php
function generatePassword($intNumOfChars) {   
    if (is_numeric($intNumOfChars) && ($intNumOfChars > 0)) {
	$strChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_";

	for ($i = 0; $i < $intNumOfChars; $i ++)  {
	    $strPassword .= $strChars[rand(0, strlen($strChars)-1)];
	 }
    }
    return $strPassword;
}
?>

0 comments for PHP - Generate a random password based on length

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.
Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.