1 回答
TA贡献1900条经验 获得超5个赞
例如,您可以将生成器声明为函数,然后在循环中多次调用它。
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_of_passwords为所需的数字或让用户在表单中选择,然后将其作为$_POST["amount"]示例传递。
- 1 回答
- 0 关注
- 155 浏览
添加回答
举报