|
Password Generator | Advanced | Memorable | Passphrase Generator | MD5 Generator | SHA-256 | Widget |
PHP Password GeneratorPHP Password Generator to generate a random password with PHP. Following is a guide on how to create a strong password generator using PHP. 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 PHPWe 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 CodeNow, 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 PHPAfter 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 |
|
|