Skip to content

Instantly share code, notes, and snippets.

@chrishaensel
Last active August 4, 2017 00:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrishaensel/abcbb902c5a08ce3421910dfcb1ec5ad to your computer and use it in GitHub Desktop.
Save chrishaensel/abcbb902c5a08ce3421910dfcb1ec5ad to your computer and use it in GitHub Desktop.
<?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