Home > database >  PHP password generator with character selection
PHP password generator with character selection

Time:11-12

I'm trying to make a small password generator with a choice of characters. In general, it turned out, but I can't connect special characters here, it just returns an empty string. What am I doing wrong?

pass-gen.php

<?php
$hidden = $_POST['hidden'];
$form_a_z = $_POST['form_a_z'];
$form_ab_zb = $_POST['form_ab_zb'];
$form_zerro_one = $_POST['form_zerro_one'];
$form_special = $_POST['form_special'];
$form_amount_symbols_pass = $_POST['form_amount_symbols_pass'];
$form_amount_pass = $_POST['form_amount_pass'];

if ($form_amount_symbols_pass > "64") $form_amount_symbols_pass = "64";
if ($form_amount_pass > "128") $form_amount_pass = "128";

if ($hidden != "") {
    print('<div style="width:600px; padding:10px;"><font style="text-indent:7px; text-decoration:none; font-size:15px; font-weight:bold; color:#0094db;">Result<br><br></font>');

    if ($form_a_z == 'y' || $form_ab_zb == 'y' || $form_zerro_one == 'y' || $form_special == 'y') {
        if ($form_a_z == 'y') $az = range("a", "z");
        else $az = array();
        if ($form_ab_zb == 'y') $az_big = range("A", "Z");
        else $az_big = array();
        if ($form_zerro_one == 'y') $zerro_one = range(0, 9);
        else $zerro_one = array();
        if ($form_special == 'y') $special = '!@#$%^&*()-_ =~{}[]:;?<>';
        else $special = array();

        $need_symbols = array_merge($az, $az_big, $zerro_one, $special);
        $count_symbols = count($need_symbols);

        for ($i = '0'; $i < $form_amount_pass; $i  ) {
            for ($a = '0'; $a < $form_amount_symbols_pass; $a  ) {
                $max_gen = $count_symbols - 1;
                $gen_sym = mt_rand(0, $max_gen);
                $symbol = $need_symbols[$gen_sym];
                $password = "$password$symbol";
            }
            print("$password<br>");
            unset($password);
        }
    } else print('No characters selected for generation');
    print('</div>');
}
?>

index.php

<table>
    <form name="forma" action="index.php" method="post">
        <input type="hidden" name="hidden" value="y" />
        <tr>
            <td>Use symbols a-z - </td>
            <td valign="bottom"><input type="checkbox" name="form_a_z" value="y" checked /></td>
        </tr>
        <tr>
            <td>Use symbols A-Z - </td>
            <td valign="bottom"><input type="checkbox" name="form_ab_zb" value="y" checked /></td>
        </tr>
        <tr>
            <td>Use symbols 0-9 - </td>
            <td valign="bottom"><input type="checkbox" name="form_zerro_one" value="y" checked /></td>
        </tr>
        <tr>
            <td>Use special characters - </td>
            <td valign="bottom"><input type="checkbox" name="form_special" value="y" checked /></td>
        </tr>
        <tr>
            <td>Number of characters in passwords (before 64) - </td>
            <td valign="bottom"><input type="text" style="width:30px;" name="form_amount_symbols_pass" size="5"
                    maxlenght="5" value="16" /></td>
        </tr>
        <tr>
            <td>Number of passwords (before 128) - </td>
            <td valign="bottom"><input type="text" style="width:30px;" name="form_amount_pass" size="5" maxlenght="5"
                    value="1" /></td>
        </tr>

        <tr>
            <td><input type="submit" value="Generate" />
    </form>
</table>

CodePudding user response:

Use str_split. This will create an array of the characters specified in the string.

if ($form_special == 'y') $special = '!@#$%^&*()-_ =~{}[]:;?<>';
if ($form_special == 'y') $special = str_split('!@#$%^&*()-_ =~{}[]:;?<>');
  • Related