dongxia2030 2014-02-24 16:48
浏览 69
已采纳

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

I have edited some code that I found online to generate a random string in PHP which I am going to use as an 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>

Below is some sample output:

9hx2qwuvjbyn18tceosp75gkf30mrli4
tzqujok5xnr783wem6sv2afcgi9h1d0y
87bxc9vr6sewaoltkni24jdgh3z1fu05
j2shgmd4o6w09yx85avp1iflqubtk3nc
0r1q9engl43uv5poxfjd6zi8cha72ytk

If I try the same again but this time increasing the length to 64 (so to avoid collision) it only produces a string of length 36 (Its maximum as the elements are NOT repeating). How can I make it so that it can choose anything from the arrays?

  • 写回答

1条回答 默认 最新

  • douxie0824 2014-02-24 16:53
    关注

    You can use the openssl extension for that:

    $secret = bin2hex(openssl_random_pseudo_bytes($length * 2));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器