PHP code for a password generator. You are able to select the password length and whether you would like to include lowercase and uppercase letters, numbers and special characters. It's random, so sometimes you see special characters, numbers etc and other times you don't - even if you have those boxes ticked.

Characters like 1, l, 0, o, O etc are removed as these quite often get people in a muddle!

The working example linked below is rendered using Zurb's Foundation framework. The code below is standard HTML.

WORKING EXAMPLE

Firstly, build the form.

<form action="#" method="post">
<p>
<label for="fader">Length</label>
<input type='range' value='<?php if (isset($POST[' length '])){ echo $POST['length ']; } else { echo "8"; } ?>' name='length' id='length' min='1' max='20' oninput="outputUpdate(value)">
<output for="length" id="displaylegth">10</output>
</p>
<p>
<input id="alpha" name="alpha" value="checked" type="checkbox" <?php if (isset($POST[ 'submit'])) { echo $_POST[ 'alpha']; } else { echo "checked"; } ?>>
<label for="alpha">Lowercase</label>

<input id="alphaupper" name="alphaupper" value="checked" type="checkbox" <?php if (isset($POST[ 'submit'])) { echo $POST[ 'alphaupper']; } else { echo "checked"; } ?>> <label for="alphaupper">Uppercase</label> </p> <p> <input id="numeric" name="numeric" value="checked" type="checkbox" <?php if (isset($POST[ 'submit'])) { echo $POST[ 'numeric']; } else { echo "checked"; } ?>> <label for="numeric">Numbers</label>

<input id="special" name="special" value="checked" type="checkbox" <?php if (isset($POST[ 'submit'])) { echo $POST[ 'special']; } else { echo "checked"; } ?>> <label for="special">Special Chars</label> </p> <p> <input type="submit" name="submit" value="Generate" class="button tiny secondary"> </p> </form>

Secondly, the PHP code to build the random password

<?php
$alpha = "abcdefghjkmnpqrstuvwxyz";
$alphaupper = strtoupper($alpha);
$numeric = "23456789";
$special = "-=!@$#*";
$chars = "";
if (isset($
POST['length'])){
if (isset($POST['alpha']) && $POST['alpha'] == 'checked') $chars .= $alpha;
if (isset($POST['alphaupper']) && $POST['alphaupper'] == 'checked') $chars .= $alphaupper;
if (isset($
POST['numeric']) && $POST['numeric'] == 'checked') $chars .= $numeric;
if (isset($
POST['special']) && $POST['special'] == 'checked') $chars .= $special; $length = $POST['length']; }else{ $chars = $alpha . $alphaupper . $numeric; $length = 9; } $len = strlen($chars); $pw = '';
for ($i=0;$i<$length;$i++) $pw .= substr($chars, rand(0, $len-1), 1); $pw = str
shuffle($pw);
echo "<input class='password' id='password' value='".$pw."'>"; ?>

Lastly, add the JS to display the password length value next to the slider.

<script>
function outputUpdate(vol) {
document.querySelector('#display_legth').value = vol; } </script>