我如何添加用户可以决定他想要多少个密码的功能?

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


我试过了,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>

<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)];

}


莫回无
浏览 156回答 1
1回答

梵蒂冈之花

例如,您可以将生成器声明为函数,然后在循环中多次调用它。function generatePassword($password_length) {&nbsp;&nbsp;&nbsp; &nbsp; $numbs = array("0","1","2",&nbsp; &nbsp; "3","4","5","6","7","8","9","@","!","$",&nbsp; &nbsp; "%","&","?",",",".","-",":","_","#","+","*","q","w","e",&nbsp; &nbsp; "r","t","z","u","i","o","p","a","s",&nbsp; &nbsp; "d","f","g","h","j","k","l","y","x","c","v","b","n","m","Q","W","E",&nbsp; &nbsp; "R","T","Z","U","I","O","P","A","S",&nbsp; &nbsp; "D","F","G","H","J","K","L","Y","X","C","V","B","N","M");&nbsp; &nbsp; shuffle($numbs);&nbsp; &nbsp; $output = "";&nbsp; &nbsp; for($i=0;$i<$password_length;$i++){&nbsp; &nbsp; &nbsp; &nbsp; $output .= $numbs[array_rand($numbs)];&nbsp; &nbsp; }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编辑:根据您的要求,这是将其合并到代码中的方式:<?phpfunction generatePassword($password_length) { //Function to generate the password&nbsp; &nbsp; $numbs = array("0","1","2",&nbsp; &nbsp; "3","4","5","6","7","8","9","@","!","$",&nbsp; &nbsp; "%","&","?",",",".","-",":","_","#","+","*","q","w","e",&nbsp; &nbsp; "r","t","z","u","i","o","p","a","s",&nbsp; &nbsp; "d","f","g","h","j","k","l","y","x","c","v","b","n","m","Q","W","E",&nbsp; &nbsp; "R","T","Z","U","I","O","P","A","S",&nbsp; &nbsp; "D","F","G","H","J","K","L","Y","X","C","V","B","N","M");&nbsp; &nbsp; shuffle($numbs);&nbsp; &nbsp; $output = "";&nbsp; &nbsp; for($i=0;$i<$password_length;$i++){&nbsp; &nbsp; &nbsp; &nbsp; $output .= $numbs[array_rand($numbs)];&nbsp; &nbsp; }&nbsp; &nbsp; return $output;}if(isset($_POST["lenght"]) { // If user submitted a form&nbsp; &nbsp; for($i=0;$i<=$amount_of_passwords-1;$i++) { // Repeat generating desired number of times&nbsp; &nbsp; &nbsp; &nbsp; $password = generatePassword($_POST["length"]);//Regex match on $passwordif (!preg_match('/^((?=.*?[A-Z])(?=.*?[a-z])(?=.*?\d)|(?=.*?[A-Z])(?=.*?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [a-z])(?=.*?[^a-zA-Z0-9])|(?=.*?[A-Z])(?=.*?\d)(?=.*?[^a-zA-Z0-9])|(?=.*?[a-z])(?=.*?\d)(?=.*?[^a-zA-Z0-9])).{8,}$/', $password)){&nbsp; &nbsp;#&nbsp; (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>";&nbsp; &nbsp; }}?>这将生成所需数量的密码,根据您的正则表达式检查它们是否有效并按照您的方式打印它们。您只需要替换$amount_of_passwords为所需的数字或让用户在表单中选择,然后将其作为$_POST["amount"]示例传递。
打开App,查看更多内容
随时随地看视频慕课网APP