Php 如何添加用户可以决定需要多少密码的功能?

Php 如何添加用户可以决定需要多少密码的功能?,php,html,regex,Php,Html,Regex,我为我的公司做了一个密码生成器,现在我想添加一个功能,用户可以决定需要多少密码 我尝试了str_repeat$output、$password_amount,但我得到了两次相同的密码,当我需要两个密码时,我的输出字段如下所示,例如123abc123abc 我是初学者。我愿意接受任何改进和指导 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtm

我为我的公司做了一个密码生成器,现在我想添加一个功能,用户可以决定需要多少密码

我尝试了str_repeat$output、$password_amount,但我得到了两次相同的密码,当我需要两个密码时,我的输出字段如下所示,例如123abc123abc

我是初学者。我愿意接受任何改进和指导

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang=de>
<head>
<title>Passwort Generator/Tester | </title>
<meta http-equiv="Content-Type" content="text/html"charset="utf-8"/>
<link rel="stylesheet" type="text/css" href="PasswortStyle.css" />
<link rel="shortcut icon" href="Logo.ico"/>
<img class="logo" src="logo.png" alt="Logo"/>



<script>
  function myFunction() {

  var copyText = document.getElementById("password");

  copyText.select();
  copyText.setSelectionRange(0, 99999);

  document.execCommand("copy");
}
</script>

</head>
<body>

<h1 id="firstHeading">Passwort-Generator</h1>
<div class="passwordgen">

<?php
    $numbs = array("0","1","2",
    "3","4","5","6","7","8","9","@","!","$",
    "%","&","?",",",".","-",":","_","#","+","*","q","w","e",
    "r","t","z","u","i","o","p","a","s",
    "d","f","g","h","j","k","l","y","x","c","v","b","n","m","Q","W","E",
    "R","T","Z","U","I","O","P","A","S",
    "D","F","G","H","J","K","L","Y","X","C","V","B","N","M");
    shuffle($numbs);

    $password_amount = 1;
    if (isset ($_POST['amount']) ) $password_amount = ($_POST['amount']) ;

    $password_length = 10;
    if (isset ($_POST['lenght']) ) $password_length = ($_POST['lenght']) ;
    $output = "";

    for($i=0;$i<$password_length;$i++){
    $output .= $numbs[array_rand($numbs)];
}



if (!preg_match('/^((?=.*?[A-Z])(?=.*?[a-z])(?=.*?\d)|(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[^a-zA-Z0-9])|(?=.*?[A-Z])(?=.*?\d)(?=.*?[^a-zA-Z0-9])|(?=.*?[a-z])(?=.*?\d)(?=.*?[^a-zA-Z0-9])).{8,}$/', $output)){   #  (1,2,3|1,2,4|1,3,4|2,3,4) 4 mögliche Kombinationen
    $message = "<span style=\"color:red\">Das Passwort entspricht nicht den <a style=\"color:red; text-decoration:underline;\" href=\"http://Passwortrichtlinien\">SKOPOS-Richtlinien</a></span>";
    } else{
    $message = "<span style=\"color:green\">Das Passwort entspricht den <a style=\"color:green; text-decoration:underline;\" href=\"http://Passwortrichtlinien\">SKOPOS-Richtlinien</a></span>";
    }

    $size = round($password_length * 1.2);
    echo"<center>Zufälliges Passwort:<input type = \"text\" id= \"password\" value=\"".$password."\" size=\"".$size."\" style=\"font-size:16pt; text-align:center; margin: 17px; font-family=\"Monaco, monospace;\" \" /><br/>
    ".$message."
    </center>";

  ?>
<center>
  <form method="post">
   <p>Passwortlänge: <input type="number" name="lenght" min="1" max="100" value="<?= $password_length?>"/> Anzahl an Passwörter: <input type="number" name="count" min="1" value="<?= $password_amount?>"/></p>
   <p><button class="button" type="submit">Neues Passwort</button></p>
   <p><button class="button" onclick="myFunction()">Passwort in Zwischenablage kopieren</button></p>
  </form>
</center>
</div>

例如,您可以将生成器声明为函数,然后在循环中多次调用它

function generatePassword($password_length) {  
    $numbs = array("0","1","2",
    "3","4","5","6","7","8","9","@","!","$",
    "%","&","?",",",".","-",":","_","#","+","*","q","w","e",
    "r","t","z","u","i","o","p","a","s",
    "d","f","g","h","j","k","l","y","x","c","v","b","n","m","Q","W","E",
    "R","T","Z","U","I","O","P","A","S",
    "D","F","G","H","J","K","L","Y","X","C","V","B","N","M");
    shuffle($numbs);
    $output = "";

    for($i=0;$i<$password_length;$i++){
        $output .= $numbs[array_rand($numbs)];
    }
return $output;
}
把它叫做:

$passwords = [];

for($i=0;$i<=$_POST["amount"]-1;$i++) // Repeat generating desired number of times
$passwords[] = generatePassword($_POST["length"]);
var_dump($passwords); //This is the array of your passwords
编辑:根据您的请求,以下是您将其合并到代码中的方式:

<?php
function generatePassword($password_length) { //Function to generate the password
    $numbs = array("0","1","2",
    "3","4","5","6","7","8","9","@","!","$",
    "%","&","?",",",".","-",":","_","#","+","*","q","w","e",
    "r","t","z","u","i","o","p","a","s",
    "d","f","g","h","j","k","l","y","x","c","v","b","n","m","Q","W","E",
    "R","T","Z","U","I","O","P","A","S",
    "D","F","G","H","J","K","L","Y","X","C","V","B","N","M");
    shuffle($numbs);
    $output = "";

    for($i=0;$i<$password_length;$i++){
        $output .= $numbs[array_rand($numbs)];
    }
    return $output;
}

if(isset($_POST["lenght"]) { // If user submitted a form
    for($i=0;$i<=$amount_of_passwords-1;$i++) { // Repeat generating desired number of times
        $password = generatePassword($_POST["length"]);

//Regex match on $password
if (!preg_match('/^((?=.*?[A-Z])(?=.*?[a-z])(?=.*?\d)|(?=.*?[A-Z])(?=.*?            [a-z])(?=.*?[^a-zA-Z0-9])|(?=.*?[A-Z])(?=.*?\d)(?=.*?[^a-zA-Z0-9])|(?=.*?[a-z])(?=.*?\d)(?=.*?[^a-zA-Z0-9])).{8,}$/', $password)){   #  (1,2,3|1,2,4|1,3,4|2,3,4) 4 mögliche Kombinationen
$message = "<span style=\"color:red\">Das Passwort entspricht nicht den <a style=\"color:red; text-decoration:underline;\" href=\"http://Passwortrichtlinien\">SKOPOS-Richtlinien</a></span>";
} else{
$message = "<span style=\"color:green\">Das Passwort entspricht den <a style=\"color:green; text-decoration:underline;\" href=\"http://Passwortrichtlinien\">SKOPOS-Richtlinien</a></span>";
}

$size = round($_POST["lenght"] * 1.2);
echo"<center>Zufälliges Passwort:<input type = \"text\" id= \"password\" value=\"".$password."\" size=\"".$size."\" style=\"font-size:16pt; text-align:center; margin: 17px; font-family=\"Monaco, monospace;\" \" /><br/>
".$message."
</center>";
    }
}
?>

这将生成所需数量的密码,根据正则表达式检查密码是否有效,并按原样打印。您只需将$amount\u密码替换为所需的数字,或者让用户在表单中选择,然后将其作为$amount POST[amount]传递即可。

您所说的密码数是什么意思?为什么用户应该有多个密码?str_repeat函数将字符串重复指定的次数。你多次获得相同的密码是很正常的。@Nico Haase对于其他公司的一些项目,我们需要很多密码,例如,当我们为一群人或某事创建帐户时。那么,输出10或100个密码比只在后面复制一个密码容易吗?好的,谢谢,我想这会解决我的问题,但是我如何在我的代码中包含你的代码呢?我试图把它包含进去,但我不明白。请看我最后的编辑。
function generatePassword($password_length) {  
    $numbs = array("0","1","2",
    "3","4","5","6","7","8","9","@","!","$",
    "%","&","?",",",".","-",":","_","#","+","*","q","w","e",
    "r","t","z","u","i","o","p","a","s",
    "d","f","g","h","j","k","l","y","x","c","v","b","n","m","Q","W","E",
    "R","T","Z","U","I","O","P","A","S",
    "D","F","G","H","J","K","L","Y","X","C","V","B","N","M");
    shuffle($numbs);
    $output = "";

    for($i=0;$i<$password_length;$i++){
        $output .= $numbs[array_rand($numbs)];
    }
return $output;
}