#php #html #random #passwords
Вопрос:
Я пытаюсь создать небольшой генератор паролей с выбором символов. В общем, получилось, но я не могу подключить здесь специальные символы, это просто возвращает пустую строку. Что я делаю не так?
pass-gen.php
lt;?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 gt; "64") $form_amount_symbols_pass = "64"; if ($form_amount_pass gt; "128") $form_amount_pass = "128"; if ($hidden != "") { print('lt;div style="width:600px; padding:10px;"gt;lt;font style="text-indent:7px; text-decoration:none; font-size:15px; font-weight:bold; color:#0094db;"gt;Resultlt;brgt;lt;brgt;lt;/fontgt;'); 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 = '!@#$%^amp;*()-_ =~{}[]:;?lt;gt;'; else $special = array(); $need_symbols = array_merge($az, $az_big, $zerro_one, $special); $count_symbols = count($need_symbols); for ($i = '0'; $i lt; $form_amount_pass; $i ) { for ($a = '0'; $a lt; $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("$passwordlt;brgt;"); unset($password); } } else print('No characters selected for generation'); print('lt;/divgt;'); } ?gt;
index.php
lt;tablegt; lt;form name="forma" action="index.php" method="post"gt; lt;input type="hidden" name="hidden" value="y" /gt; lt;trgt; lt;tdgt;Use symbols a-z - lt;/tdgt; lt;td valign="bottom"gt;lt;input type="checkbox" name="form_a_z" value="y" checked /gt;lt;/tdgt; lt;/trgt; lt;trgt; lt;tdgt;Use symbols A-Z - lt;/tdgt; lt;td valign="bottom"gt;lt;input type="checkbox" name="form_ab_zb" value="y" checked /gt;lt;/tdgt; lt;/trgt; lt;trgt; lt;tdgt;Use symbols 0-9 - lt;/tdgt; lt;td valign="bottom"gt;lt;input type="checkbox" name="form_zerro_one" value="y" checked /gt;lt;/tdgt; lt;/trgt; lt;trgt; lt;tdgt;Use special characters - lt;/tdgt; lt;td valign="bottom"gt;lt;input type="checkbox" name="form_special" value="y" checked /gt;lt;/tdgt; lt;/trgt; lt;trgt; lt;tdgt;Number of characters in passwords (before 64) - lt;/tdgt; lt;td valign="bottom"gt;lt;input type="text" style="width:30px;" name="form_amount_symbols_pass" size="5" maxlenght="5" value="16" /gt;lt;/tdgt; lt;/trgt; lt;trgt; lt;tdgt;Number of passwords (before 128) - lt;/tdgt; lt;td valign="bottom"gt;lt;input type="text" style="width:30px;" name="form_amount_pass" size="5" maxlenght="5" value="1" /gt;lt;/tdgt; lt;/trgt; lt;trgt; lt;tdgt;lt;input type="submit" value="Generate" /gt; lt;/formgt; lt;/tablegt;
Ответ №1:
Воспользуйся str_split
. Это создаст массив символов, указанных в строке.
if ($form_special == 'y') $special = '!@#$%^amp;*()-_ =~{}[]:;?lt;gt;';
if ($form_special == 'y') $special = str_split('!@#$%^amp;*()-_ =~{}[]:;?lt;gt;');