Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrishaensel/a80f8b91d76809a2cdd34facc5ef878f to your computer and use it in GitHub Desktop.
Save chrishaensel/a80f8b91d76809a2cdd34facc5ef878f to your computer and use it in GitHub Desktop.
Human Readable Password Generator
<?php
function randomPassword( $len = 8, $ucfirst = false, $spchar = false ){
/* Programmed by Christian Haensel
* christian@chftp.com
* http://www.chftp.com
*
* Exclusively published on weberdev.com.
* If you like my scripts, please let me know or link to me.
* You may copy, redistribute, change and alter my scripts as
* long as this information remains intact.
*
* Modified by Josh Hartman on 2010-12-30.
* Last modified: 2019-10-04
* Thanks to JKDos for suggesting improvements.
*/
if ( $len >= 6 && ( $len % 2 ) !== 0 ) { // Length parameter must be greater than or equal to 6, and a multiple of 2
$len = 8;
}
$length = $len - 2; // Makes room for a 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');
$spchars = array('!','@','#','$','%','^','*','&','*','-','+','?');
$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 ( $spchar == true ) {
$password = substr($password, 0, -1) . $spchars[ rand( 0, 11 ) ];
}
$password .= rand( 10, 99 );
if($ucfirst==true){
$password = ucfirst( $password );
}
return $password;
}
echo randomPassword();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment