为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
莫回无 2022-01-02 16:25:42
我为我的公司制作了一个密码生成器,现在我想添加用户可以决定他想要多少个密码的功能。我试过了,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)];}
查看完整描述

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"]示例传递。


查看完整回答
反对 回复 2022-01-02
  • 1 回答
  • 0 关注
  • 155 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信