Skip to content

Instantly share code, notes, and snippets.

@joshhartman
Last active July 7, 2024 09:12
Show Gist options
  • Save joshhartman/1507069 to your computer and use it in GitHub Desktop.
Save joshhartman/1507069 to your computer and use it in GitHub Desktop.
Human Readable Password Generator (Requires PHP 7.1+)
<?php
function randomPassword( $len = 10, $ucfirst = true, $spchar = true ){
/* Programmed by Christian Haensel
* [email protected]
* 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: 2023-03-01
* Thanks to JKDos for suggesting improvements.
*/
if ( $len >= 8 && ( $len % 2 ) !== 0 ) { // Length parameter must be greater than or equal to 8, 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' );
$vowel = 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 .= $vowel[ random_int( 0, 4 ) ];
}
if ( $spchar == true ) {
$password = substr( $password, 0, - 1 ) . $spchars[ random_int( 0, 12 ) ];
}
$password .= random_int( 10, 99 );
if ( $ucfirst == true ) {
$password = ucfirst( $password );
}
return $password;
}
echo randomPassword();
@chrishaensel
Copy link

It is so funny to see my old script still floating around somewhere 👍

@JKDos
Copy link

JKDos commented Jul 1, 2019

I found your program, and I like it. I work in IT, so I am often creating passwords for users. If I could make one suggestion, I would capitalize the first letter, and add a random special character at the end ( ! @ # $ % ^ & * + - ).

@JKDos
Copy link

JKDos commented Jul 30, 2019

I copied the source to my own website for the heck of it. I've included some special characters, and increased the pool of consonants and vowels. That is

Consonant: "sh","sn","tr","th","ch"
Vowels: "ee","oo","ie","au"

Also,I've used a substr to control the length better. $password = substr($password,0,$len-3); This allows me to use those double character values, and a special symbol while still controlling the exact password length.

And first letter is capitalized $newpass = strtoupper(substr($password,0,1)) . substr($password,1,strlen($password));

@joshhartman
Copy link
Author

Nice additions, thanks for sharing @JKDos

@JKDos
Copy link

JKDos commented Aug 2, 2019

You're welcome. Anyone can check it out as-well-as the source by checking: https://jkdos.com/apps/php/passwd-gen/

@jaysonwcs
Copy link

It is so funny to see my old script still floating around somewhere 👍

It's simple and efficient! 👏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment