Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
<?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