PHP生成随机密码 - php

我整理了这个小脚本,它使用单词列表来生成可读的密码。

通过生成单词和数字,代码的第一部分可以正常工作。问题在于,最后的符号似乎只能经常使用。

 <?php
function random_readable_pwd($length=10){

    // the wordlist from which the password gets generated 
    // (change them as you like)
    $words = 'AbbyMallard,AbigailGabble,AbisMal,Abu,Adella,TheAgent,AgentWendyPleakley,Akela,AltheAlligator,Aladar,Aladdin,AlamedaSlim,AlanaDale,Alana,Alcmene,Alice,AmeliaGabble,AmosSlade,Amphitryon,AnastasiaTremaine,Anda,Andrina,Angelique,AngusMacBadger';

    // Split by ",":
    $words = explode(',', $words);
    if (count($words) == 0){ die('Wordlist is empty!'); }

    // Add words while password is smaller than the given length
    $pwd = '';
    while (strlen($pwd) < $length){
        $r = mt_rand(0, count($words)-1);
        $pwd .= $words[$r];
    }

    $num = mt_rand(1, 99);
     if ($length > 2){
        $pwd = substr($pwd,0,$length-strlen($num)).$num;
    } else { 
        $pwd = substr($pwd, 0, $length);
    }

   $pass_length = strlen($pwd);
   $random_position = rand(0,$pass_length);

   $syms = "!@#$%^&*()-+?";
   $int = rand(0,51);
   $rand_char = $syms[$int];

   $pwd = substr_replace($pwd, $rand_char, $random_position, 0);

    return $pwd;
}


?>
<html><head><title>Password generator</title></head>
<body>
<h1>Password generator</h2>

<p>
<?php 
echo random_readable_pwd(10);
?>
</p>

</body>
</html>

参考方案

您似乎获得了0到51之间的随机数,但是$ syms字符串中只有13个字符。它应该是:

$syms = "!@#$%^&*()-+?";
$int = rand(0,12);
$rand_char = $syms[$int];

还没有测试过,但是我认为这是问题所在。

甚至更好,获取string's length:

$syms = "!@#$%^&*()-+?";
$int = rand(0,strlen($syms)-1);
$rand_char = $syms[$int];

php Singleton类实例将在多个会话中保留吗? - php

举一个简单的例子,如果我想计算一个不使用磁盘存储的脚本的命中次数,我可以使用静态类成员来执行此操作吗?用户1:<?php $test = Example::singleton(); $test->visits++; ?> 用户2:<?php $test = Example::singleton(); $test->visits+…

php:将分钟取整到最近的四分之一小时,然后执行更多操作 - php

最初的问题是这样的:取分钟数->转换为四分之一小时-> 1个四分之一小时为1个单位->输出单位我今天整天都在整理页面,几分钟前我的大脑就停止工作了,我只是不知道如何输出单位数量。我知道在此网站上发布问题会有所帮助。因此,用户输入的分钟数(不是小时和分钟,而是数分钟),站点需要输出单位数量。单位是一个刻钟。分钟总是四舍五入到最近的四分之一小时…

PHP OOP概念付诸实践 - php

目前,我所拥有的是一份以程序庄园形式编写的审查表。我想更改一些内容,并希望在此过程中,根据过去的经验,在OOP中,网站的整个审核部分会更好。数据流如下:HTML表单=>提交=> jQuery验证=>? => PHP验证=>添加记录我的问题是要了解将POST数据传递给类的正确方法是使用AJAX / jQuery或使用具有标准感谢页…

PHP-将日期插入日期时间字段 - php

我已在数据库中使用datetime字段存储日期,使用PHP将“今天的日期”插入该字段的正确方法是什么?干杯, 参考方案 我认为您可以使用php date()函数

PHP-如何获取类的成员函数列表? - php

如果我知道班级的名字。有没有办法知道类的成员函数列表? 参考方案 get_class_methods()是你的朋友