【发布时间】:2019-09-06 07:25:19
【问题描述】:
我为我的公司制作了一个密码生成器,现在我想添加用户可以决定他想要多少个密码的功能。
我尝试了str_repeat ($output , $password_amount ),但是我两次获得相同的密码,当我想要 2 个密码时,我的输出字段看起来像这样,例如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>
【问题讨论】:
-
“多少个密码”是什么意思?为什么一个用户应该有多个密码?
-
str_repeat() 函数将字符串重复指定的次数。多次获得相同的密码是正常的..
-
@Nico Haase 对于其他公司的一些项目,我们需要很多密码,例如当我们为一群人创建帐户时。那么输出 10 个或 100 个密码比只复制一个密码更容易吗?