PHP Password Generator

PHP Password Generator to generate a random password with PHP. Following is a guide on how to create a strong password generator using PHP.

Random Password:

PHP Random Password Generator

Password Length:
Include Lowercase Characters:
Include Uppercase Characters:
Include Numbers:
Include Symbols:
Exclude Similar Characters:
Exclude Ambiguous Characters:

How to create a password generator in PHP?

This a guide on how to create a password generator using PHP which allows users to generate secure passwords on a server. There are two main parts of the password generator, the complexity and the length of a password. In this PHP guide, users can define the length of the password, and what characters, numbers, or symbols to be used in their passwords.


Customize user password in PHP

We want to give users the option to choose the length of their passwords, and if they want to include lowercase letters, uppercase letters, numbers, symbols in their passwords. Users can also choose to exclude similar characters and ambiguous characters from their passwords. Below is the html code that takes user input to generate their passwords.

<form method="post" action="https://passwords-generator.org/php">     
	<table class="table table-bordered" style="table-layout:fixed">
        <tr>
            <td width="40%">Random Password:</td> 
            <td style="word-wrap: break-word"><input name="txtPassword" type="text" value="<?php echo $password; ?>" 
			readonly="readonly" id="txtPassword" /></td>
        </tr>       
    </table>
    <table class="table table-bordered">
        <tr class="header2">
            <td colspan="2"><h2>PHP Random Password Generator</h2></td>
        </tr>    
    <tr>
        <td width="40%">Password Length:</td>
        <td><input name="txtPasswordLength" type="text" value="<?php echo $txtPasswordLength; ?>" 
		id="txtPasswordLength" onkeyup="generate()" /></td>
    </tr>     
    <tr>
        <td>Include Lowercase Characters:</td>
        <td><span onchange="generate()"><input id="chkIncludeLowerChar" type="checkbox" name="chkIncludeLowerChar"
		<?php if($chkIncludeLowerChar) { echo "checked=\"checked\""; } ?> />e.g. abcdef)/td>
    </tr>
    <tr>
        <td>Include Uppercase Characters:</td>
        <td><span onchange="generate()"><input id="chkIncludeUpperChar" type="checkbox" name="chkIncludeUpperChar" 
		<?php if($chkIncludeUpperChar) { echo "checked=\"checked\""; } ?> />(e.g. ABCDEF)</td>
    </tr>  
    <tr>
        <td>Include Numbers:</td>
        <td><span onchange="generate()"><input id="chkIncludeNumbers" type="checkbox" name="chkIncludeNumbers" 
		<?php if($chkIncludeNumbers) { echo "checked=\"checked\""; } ?> />(e.g. 123456)</td>
    </tr>       
    <tr>
        <td>Include Symbols:</td>
        <td><span onchange="generate()"><input id="chkIncludeSymbols" type="checkbox" name="chkIncludeSymbols"
		<?php if($chkIncludeSymbols) { echo "checked=\"checked\""; } ?> />(e.g. *@#+%)</td>
    </tr>
    <tr>
        <td>Exclude Similar Characters:</td>
        <td><span onchange="generate()"><input id="chkExcludeSimilar" type="checkbox" name="chkExcludeSimilar"
		<?php if($chkExcludeSimilar) { echo "checked=\"checked\""; } ?> />(e.g. o,0,i,l,1)</td>
    </tr>
    <tr>
        <td>Exclude Ambiguous Characters:</td>
        <td><span onchange="generate()"><input id="chkExcludeAmbiguous" type="checkbox" name="chkExcludeAmbiguous"
		<?php if($chkExcludeAmbiguous) { echo "checked=\"checked\""; } ?> />(e.g. ~,;:.{}<>[]()/\'`)</td>
    </tr>
        <tr>
            <td></td>
            <td><input id="calculate-submit" type="submit" name="submit" value="Generate Password"></td>
      </tr> 
   </table>
</form>    

Password Generator PHP Code

Now, we have our html form ready. Let's move on the the PHP code that generates the password based on the user input. We will need a few array variables for each of the user inputs that defines what type of characters, numbers, and symbols to use for their passwords and which ones to exclude if there is any.

$upperChars = array("A","B","C","D","E","F","G","H","J","K","M","N","P","Q","R","S","T","U","V","W","X","Y","Z");
$lowerChars = array("a","b","c","d","e","f","g","h","j","k","m","n","p","q","r","s","t","u","v","w","x","y","z");
$numbers = array("2","3","4","5","6","7","8","9");
$symbols = array("!","#","$","%","&","*","+","-","?","@");
$similars_lower = array("i","l","o");
$similars_upper = array("I","L","O");
$similars_numbers = array("1","0");
$similars_symbols = array("|");
$ambiguous = array("\"","'","(",")",",",".","/",":",";","<","=",">","[","\\","]","^","_","`","{","}","~");

Some of the arrays are the characters that we need for our password, and other arrays are the ones to exlucde.

Generating the password in PHP

After we have our arrays ready, here comes the most fun part of generating a password in PHP.

The logic is to generate a random item from our arrays as each character of the password, and at the end we will combine all the characters to form a strong password.

In order to do that, we will need to use a for loop that walks through each character of the password based on the length.

More importantly, our password should meet all the user criteria, such as lowercase, uppercase charactes, and digits.

To do that, we use a new array that combines all the other arrays to include only valid characters based on user input.

For example, if the user wants to include lowercase characters in his password, we will append the lower character array to our new array. On the other hand, if the user does not want to include uppercase characters in his password, we will just not append it to our new array. Once we are done setting the new array, all we have to do is to generate a random index from the new array, and pull the character that are located on that index. Here's the complete PHP code to generate a password based on user filters.

if ($_SERVER["REQUEST_METHOD"] == "POST") {
	$txtPasswordLength = $_REQUEST['txtPasswordLength'];
	$chkIncludeLowerChar = $_REQUEST['chkIncludeLowerChar'];
	$chkIncludeUpperChar = $_REQUEST['chkIncludeUpperChar'];
	$chkIncludeNumbers = $_REQUEST['chkIncludeNumbers'];
	$chkIncludeSymbols = $_REQUEST['chkIncludeSymbols'];
	$chkExcludeSimilar = $_REQUEST['chkExcludeSimilar'];
	$chkExcludeAmbiguous = $_REQUEST['chkExcludeAmbiguous'];
} else {
	$txtPasswordLength = "16";
	$chkIncludeLowerChar = 1;
	$chkIncludeUpperChar = 1;
	$chkIncludeNumbers = 1;
	$chkIncludeSymbols = 1;
	$chkExcludeSimilar = 0;
	$chkExcludeAmbiguous = 0;
}

$password="";
$array = [];
$count = 0;
if($txtPasswordLength < 1000)
{
	if($chkIncludeLowerChar){
		$array = array_merge($array, $lowerChars);
	}
	if($chkIncludeUpperChar){
		$array = array_merge($array, $upperChars);
	}
	if($chkIncludeNumbers){
		$array = array_merge($array, $numbers);
	}
	if($chkIncludeSymbols){
		$array = array_merge($array, $symbols);
	}
	if(!$chkExcludeSimilar){
		if($chkIncludeLowerChar)
		{
			$array = array_merge($array, $similars_lower);
		}
		if($chkIncludeUpperChar)
		{
			$array = array_merge($array, $similars_upper);
		}
		if($chkIncludeNumbers)
		{
			$array = array_merge($array, $similars_numbers);
		}
		if($chkIncludeSymbols)
		{
			$array = array_merge($array, $similars_symbols);
		}   
	}
	
	if(!$chkExcludeAmbiguous && $chkIncludeSymbols){
		$array = array_merge($array, $ambiguous);
	}
	$randomIndex;
	
	if(count($array) > 1)
	{
		for ($i = 0; $i < $txtPasswordLength; $i++) {
			$randomIndex = rand(0, count($array));
			$password = $password . $array[$randomIndex];
		}
	}
}
$password = htmlspecialchars($password);

That's it, that's all the code we needed to create a simple password generator in JavaScript. You can use the password generator demo on the top of this page to do some testing and see how it works in live example.

JavaScript Password Generator





Password Manager
Pincode Generator
MD5 Hash Generator
SHA-1 Hash Generator
SHA-256 Hash Generator
SHA-512 Hash Generator
SHA-384 Hash Generator
SHA-224 Hash Generator
Memorable Password Generator
Htpasswd Generator
Base64 Encode
Base64 Decode
Case Converter
Morse Code Translator
Random Letter Generator
Random Number Generator
Username Generator
Url Encode
Url Decode
Text to ASCII
ASCII to Text
Advanced Password Generator
Bulk Password Generator
Password Generator Words
How Strong is My Password
Passphrase Generator
Password Generator Widget
JavaScript Password Generator
How Secure is My Password
Kids Password Generator
12 Character Password Generator
Generador de Contraseñas
密码生成器
पासवर्ड जनरेटर
Générateur de mot de passe
비밀번호 생성기
Gerador de senhas
Генератор паролей
Passwortgenerator
Tiếng Việt
パスワードジェネレータ
Şifre Oluşturucu
generatore di password
مولد كلمة السر
Pembuat Kata Sandi
Γεννήτρια κωδικών πρόσβασης
Generátor hesiel
Generator hasła
ตัวสร้างรหัสผ่าน
Wachtwoord Generator
Jelszó generálása
تولید کننده رمز عبور
Құпия сөз генераторы
Slaptažodžių generatorius
Tagabuo ng Password
Penjana Kata Laluan
Gineadóir Pasfhocal
Generator de parolă
Lösenordsgenerator
Passordgenerator
Jenereta ya Nenosiri