PHP 带字符选择的密码生成器

PHP password generator with character selection

我正在尝试制作一个可以选择字符的小型密码生成器。一般来说,结果是,但我不能在这里连接特殊字符,它只是 returns 一个空字符串。我做错了什么?

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>

使用str_split。这将创建字符串中指定字符的数组。

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