Last active
May 16, 2024 22:05
-
-
Save chrishaensel/abcbb902c5a08ce3421910dfcb1ec5ad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Generate a random human-readable passwort | |
* Including the modifications by Josh Hartman made on 12/30/2010. https://github.com/joshhartman | |
* Josh added the ability to append a double digit number | |
* I added a parameter to decide on whether to append the number | |
* | |
* Also, if the length is not dividable by 2, the length of the password will be $length + 1 | |
* | |
* @param int $length | |
* | |
* @return string | |
*/ | |
function generateRandomPassword( $length = 16, $appendNumber = true ) { | |
if ( ( $length % 2 ) !== 0 ) { // Length paramenter must be a multiple of 2 | |
$length = $length + 1; | |
} | |
if ( $appendNumber ) { | |
$length = $length - 2; | |
}; // Makes room for the two-digit number on the end | |
$conso = array( 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z' ); | |
$vocal = array( 'a', 'e', 'i', 'o', 'u' ); | |
$password = ''; | |
srand( (double) microtime() * 1000000 ); | |
$max = $length / 2; | |
for ( $i = 1; $i <= $max; $i ++ ) { | |
$password .= $conso[ rand( 0, 19 ) ]; | |
$password .= $vocal[ rand( 0, 4 ) ]; | |
} | |
if ( $appendNumber ) { | |
$password .= rand( 10, 99 ); | |
} | |
$newpass = $password; | |
return $newpass; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment