php 在php中生成随机代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4570980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:30:35  来源:igfitidea点击:

generating a random code in php?

phprandom

提问by getaway

i know this might seem silly, but i want to generate a random code of 8 characetrs, only numbers or letters using php. i needs this to generate a password for each user that signs up, thanks

我知道这可能看起来很傻,但我想使用 php 生成一个包含 8 个字符的随机代码,只有数字或字母。我需要这个来为每个注册的用户生成一个密码,谢谢

回答by eriksv88

I would rather use md5to generate passwords

我宁愿使用md5来生成密码

But you can use something like this if you want a custom:

但是如果你想要一个自定义,你可以使用这样的东西:

function createRandomPassword() { 

    $chars = "abcdefghijkmnopqrstuvwxyz023456789"; 
    srand((double)microtime()*1000000); 
    $i = 0; 
    $pass = '' ; 

    while ($i <= 7) { 
        $num = rand() % 33; 
        $tmp = substr($chars, $num, 1); 
        $pass = $pass . $tmp; 
        $i++; 
    } 

    return $pass; 

} 

回答by Jeremy

What about something like this, for ease:

像这样的事情怎么样,为了方便:

$pass = substr(md5(uniqid(mt_rand(), true)) , 0, 8);

回答by jondavidjohn

<?php 

$uniqid = uniqid();

$rand_start = rand(1,5);

$rand_8_char = substr($uniqid,$rand_start,8);

?>

回答by Abhi

A good or efficient method for doing this is:

这样做的一个好的或有效的方法是:

public function generateRandomString($length = 8) {
    $characters = '0123456789abcdefghijklmnopqrs092u3tuvwxyzaskdhfhf9882323ABCDEFGHIJKLMNksadf9044OPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}

By changing $length variable you can generate alphanumeric code according to your need.

通过更改 $length 变量,您可以根据需要生成字母数字代码。

回答by Sean Vieira

PHP has a randfunction (and also a mt_randfunction that the docs claim is faster.)

PHP 有一个rand函数(还有一个mt_rand文档声称更快的函数。)

So do something like this:

所以做这样的事情:

$i = 0;
$pwd = "";
while ( $i < 10) {
    if (mt_rand() % 2 == 0) {
        $pwd .= rand();
    } else {
        $pwd .= char(rand());
        // http://php.net/manual/en/function.chr.php
    }
    $i += 1;
}

回答by mario

Use base64_encode(), feed it some rand()numbers, and cut off the first 8 characters, which are definitely letters or numbers. That's not a totally random combination due to the input being just integers. But it's good enough for default user-passwords. (Else use rand()via chr()before encoding.)

使用base64_encode(),给它输入一些rand()数字,并截掉前 8 个字符,这些字符肯定是字母或数字。由于输入只是整数,这不是完全随机的组合。但是对于默认用户密码来说已经足够了。(否则在编码之前使用rand()via chr()。)

回答by Mohamad Hamouday

This work like charm and you can choose the type of characters that will be generated like:
"Upper_Case", "Lower_Case", "Number", "Special_Character"

这就像魅力一样,您可以选择将生成的字符类型,例如:
“Upper_Case”、“Lower_Case”、“Number”、“Special_Character”

function Create_Random_Code($Length = 8, $IN_Params = [])
{
    $IN_Params['Upper_Case']        = isset($IN_Params['Upper_Case']) ? $IN_Params['Upper_Case'] : true;
    $IN_Params['Lower_Case']        = isset($IN_Params['Lower_Case']) ? $IN_Params['Lower_Case'] : true;
    $IN_Params['Number']            = isset($IN_Params['Number']) ? $IN_Params['Number'] : true;
    $IN_Params['Special_Character'] = isset($IN_Params['Special_Character']) ? $IN_Params['Special_Character'] : false;

    $Chars = '';
    if($IN_Params['Lower_Case'])
    {
        $Chars .= "abcdefghijklmnopqrstuvwxyz";
    }

    if($IN_Params['Upper_Case'])
    {
        $Chars .= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    }

    if($IN_Params['Number'])
    {
        $Chars .= "0123456789";
    }

    if($IN_Params['Special_Character'])
    {
        $Chars .= "!@#$%^&*()_-=+;:,.";
    }

    return substr(str_shuffle($Chars), 0, $Length);
}

To use it:

要使用它:

echo Create_Random_Code(6,[
                            'Upper_Case'        => true,
                            'Lower_Case'        => true,
                            'Number'            => true,
                            'Special_Character' => false
                        ]);

回答by Sajjad Hossain

You can use this,

你可以用这个,

function rand_code($len)
{
 $min_lenght= 0;
 $max_lenght = 100;
 $bigL = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
 $smallL = "abcdefghijklmnopqrstuvwxyz";
 $number = "0123456789";
 $bigB = str_shuffle($bigL);
 $smallS = str_shuffle($smallL);
 $numberS = str_shuffle($number);
 $subA = substr($bigB,0,5);
 $subB = substr($bigB,6,5);
 $subC = substr($bigB,10,5);
 $subD = substr($smallS,0,5);
 $subE = substr($smallS,6,5);
 $subF = substr($smallS,10,5);
 $subG = substr($numberS,0,5);
 $subH = substr($numberS,6,5);
 $subI = substr($numberS,10,5);
 $RandCode1 = str_shuffle($subA.$subD.$subB.$subF.$subC.$subE);
 $RandCode2 = str_shuffle($RandCode1);
 $RandCode = $RandCode1.$RandCode2;
 if ($len>$min_lenght && $len<$max_lenght)
 {
 $CodeEX = substr($RandCode,0,$len);
 }
 else
 {
 $CodeEX = $RandCode;
 }
 return $CodeEX;
}

Details Random code generator in PHP

详细信息PHP 中的随机代码生成器