Skip to content

Instantly share code, notes, and snippets.

@hh-lohmann
Forked from joshhartman/randomPassword.php
Created July 22, 2022 07:19
Show Gist options
  • Save hh-lohmann/31e3d61adb3ed38b4d8781620447dbd3 to your computer and use it in GitHub Desktop.
Save hh-lohmann/31e3d61adb3ed38b4d8781620447dbd3 to your computer and use it in GitHub Desktop.
Human Readable Password Generator (Requires PHP 7.1+)
<?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: 2021-10-05
* 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 = '';
$max = $length / 2;
for ( $i = 1; $i <= $max; $i ++ ) {
$password .= $conso[ random_int( 0, 19 ) ];
$password .= $vocal[ random_int( 0, 4 ) ];
}
if ( $spchar == true ) {
$password = substr($password, 0, -1) . $spchars[ random_int( 0, 11 ) ];
}
$password .= random_int( 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