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;
}
?>
Post new comment