Php 这个随机字符串生成器有什么问题?

Php 这个随机字符串生成器有什么问题?,php,arrays,string,random,generator,Php,Arrays,String,Random,Generator,我编辑了一些我在网上找到的代码,用PHP生成了一个随机字符串,我将使用它作为ID <!DOCTYPE html> <html class="no-js"> <head> <meta charset="utf-8"> <title>ID Generator</title> <link rel="stylesheet" href="idgen.css"> </head> <b

我编辑了一些我在网上找到的代码,用PHP生成了一个随机字符串,我将使用它作为ID

<!DOCTYPE html>
<html class="no-js">
<head>
    <meta charset="utf-8">
    <title>ID Generator</title>
    <link rel="stylesheet" href="idgen.css">
</head>
<body>
<?php 

class randomPassword {

    function __construct( $passType='alphaNumeric', $length=64, $rangeLength=9 ) {
        $this->passType = $this->setPassType( $passType );
        $this->setLength( $length );
        $this->setRangeLength( $rangeLength );
    }

    function setRangeLength( $rangeLength ) {
        $this->rangeLength=$rangeLength;
    }

    // set the length of the password
    private function setLength( $length ) {
        $this->length=$length;
    }

    // set the type of password
    private function setPassType( $passType ) {
        return $passType.'Chars';
    }

    // return an array of numbers
    private function numericChars() {
        return range( 0, $this->rangeLength );
    }

    // return an array of lowercase chars
    private function lcAlphaChars() {
        return range( 'a', 'z' );
    }

    //return an array of uppercase chars
    private function ucAlphaChars() {
        return range( 'A', 'Z' );
    }

    // return an array of all alpha chars
    private function alphaChars() {
        return array_merge( $this->lcAlphaChars(), $this->ucAlphaChars() );
    }

    // return an array of alphanumeric chars
    private function alphaNumericChars() {
        return array_merge( $this->alphaChars(), $this->numericChars() );
    }

    // return array of lowercase characters and numbers
    private function lcAlphaNumericChars() {
        return array_merge( $this->lcAlphaChars(), $this->numericChars() );
    }

    // return array of uppercase characters and numbers
    private function ucAlphaNumericChars() {
        return array_merge( $this->ucAlphaChars(), $this->numericChars() );
    }

    // return a string of chars
    private function makeString() {
        // set the function to call based on the password type
        $funcName = $this->passType;
        return implode( $this->$funcName() );
    }

    // shuffle the chars and return $length of chars
    public function makePassword() {
        return substr( str_shuffle( $this->makeString() ), 1, $this->length );
    }

} // end class

?>

<h3>ID Generator</h3>
<h4>LC Alpha Numeric</h4>
<?php
    for ($i=0;$i<5;$i++)
    {
        try
        {
            $obj = new randomPassword( 'lcAlphaNumeric', 32, 9 );
            echo $obj->makePassword().'<br />';
        }
        catch( Exception $ex )
        {
            echo $ex->getMessage();
        }
    }
?>
</body>
</html>

ID生成器
ID生成器
字母数字
以下是一些示例输出:

9hx2qwuvjbyn18tceosp75gkf30mrli4 tzqujok5xnr783wem6sv2afcgi9h1d0y 87bxc9vr6sewaoltkni24jdgh3z1fu05 j2shgmd4o6w09yx85avp1iflqubtk3nc 0r1q9engl43uv5poxfjd6zi8cha72ytk 9hx2qwuvjbyn18tceosp75gkf30mli4 TZQU5XNR783WEM6SV2AFCGI9H1D0Y 87BXC9VR6SEWAOLTCNI24JDGH3Z1FU05 j2shgmd4o6w09yx85avp1iflqubtk3nc 0r1q9engl43uv5poxfjd6zi8cha72ytk
如果我再次尝试相同的操作,但这次将长度增加到64(以避免冲突),它只会生成长度为36的字符串(其最大值为,元素不会重复)。如何使其能够从阵列中选择任何内容?

您可以使用
openssl
扩展:

$secret = bin2hex(openssl_random_pseudo_bytes($length * 2));

您可以使用
openssl
扩展:

$secret = bin2hex(openssl_random_pseudo_bytes($length * 2));


$obj=new randomPassword('lcAlphaNumeric',32,9)
您是否在此处将其更改为64?是的,正如我所说,它在长度36(26个字母和10个数字[0-9])处停止,因为它不会重复使用数组中的元素。按照编写方式,这是不可能的。它构建了一个可用字符数组并简单地洗牌。因为您只允许小写字母数字字符,所以不可能将36个字符的数组转换为37个以上的字符。如果不从根本上重写这个例程,就不能这样做。您通过对所有可能值的数组进行压缩来生成一个字符串,因此它最多生成36个字符——正如您所说,这就是它的长度。如果你想获得更长的密码,你需要重新编写它-比如说,将数组按所需密码长度的数倍洗牌,然后每次选择第一个字符。我能不能每次都选择一个随机元素?
$obj=new randomPassword('lcAlphaNumeric',32,9)
你在这里把它改为64了吗?是的,正如我所说的,它的长度为36(26个字母和10个数字[0-9]),因为它不重用数组中的元素。这种写入方式是不可能的。它构建了一个可用字符数组并简单地洗牌。因为您只允许小写字母数字字符,所以不可能将36个字符的数组转换为37个以上的字符。如果不从根本上重写这个例程,就不能这样做。您通过对所有可能值的数组进行压缩来生成一个字符串,因此它最多生成36个字符——正如您所说,这就是它的长度。如果你想获得更长的密码,你需要重新编写它——比如说,按照所需密码长度的多少对数组进行洗牌,然后每次选择第一个字符。我不能每次只选择一个随机元素吗?这不起作用,我会在浏览器中看到奇怪的字符。我只想允许使用字母数字字符。谢谢。5分钟后我接受。你知道我是怎么用我的方式完成的吗?谢谢:)基本上你只需要一些“好的”随机性,然后把它转换成ascii字母序列。虽然我的想法很有效,但您需要
length*2
(!!)使其足够随机。因此,如果我想要长度为64,我应该使用$length=32,并在函数中使用$length*2?不,您需要传递64,但您可以获得128个字符。这是因为我的随机生成器生成完整的随机值,而不仅仅是那些可通过ascii打印的值。这是更好的随机性,但它需要将字符串显示为十六进制,即每个字节两个字母。这不起作用,我在浏览器中看到奇怪的字符。我只想允许使用字母数字字符。谢谢。5分钟后我接受。你知道我是怎么用我的方式完成的吗?谢谢:)基本上你只需要一些“好的”随机性,然后把它转换成ascii字母序列。虽然我的想法很有效,但您需要
length*2
(!!)使其足够随机。因此,如果我想要长度为64,我应该使用$length=32,并在函数中使用$length*2?不,您需要传递64,但您可以获得128个字符。这是因为我的随机生成器生成完整的随机值,而不仅仅是那些可通过ascii打印的值。这是更好的随机性,但它需要将字符串显示为十六进制,即每个字节两个字母。