PHP: How to generate a random, unique, alphanumeric string for use in a secret link?

后端 未结 28 2102
隐瞒了意图╮
隐瞒了意图╮ 2020-11-21 22:20

How would it be possible to generate a random, unique string using numbers and letters for use in a verify link? Like when you create an account on a website, and it sends y

相关标签:
28条回答
  • 2020-11-21 23:18

    Here is ultimate unique id generator for you. made by me.

    <?php
    $d=date ("d");
    $m=date ("m");
    $y=date ("Y");
    $t=time();
    $dmt=$d+$m+$y+$t;    
    $ran= rand(0,10000000);
    $dmtran= $dmt+$ran;
    $un=  uniqid();
    $dmtun = $dmt.$un;
    $mdun = md5($dmtran.$un);
    $sort=substr($mdun, 16); // if you want sort length code.
    
    echo $mdun;
    ?>
    

    you can echo any 'var' for your id as you like. but $mdun is better, you can replace md5 to sha1 for better code but that will be very long which may you dont need.

    Thank you.

    0 讨论(0)
  • 2020-11-21 23:19

    Use the code below to generate the random number of 11 characters or change the number as per your requirement.

    $randomNum=substr(str_shuffle("0123456789abcdefghijklmnopqrstvwxyz"), 0, 11);
    

    or we can use custom function to generate the random number

     function randomNumber($length){
         $numbers = range(0,9);
         shuffle($numbers);
         for($i = 0;$i < $length;$i++)
            $digits .= $numbers[$i];
         return $digits;
     }
    
     //generate random number
     $randomNum=randomNumber(11);
    
    0 讨论(0)
  • 2020-11-21 23:19

    after reading previous examples I came up with this:

    protected static $nonce_length = 32;
    
    public static function getNonce()
    {
        $chars = array();
        for ($i = 0; $i < 10; $i++)
            $chars = array_merge($chars, range(0, 9), range('A', 'Z'));
        shuffle($chars);
        $start = mt_rand(0, count($chars) - self::$nonce_length);
        return substr(join('', $chars), $start, self::$nonce_length);
    }
    

    I duplicate 10 times the array[0-9,A-Z] and shuffle the elements, after I get a random start point for substr() to be more 'creative' :) you can add [a-z] and other elements to array, duplicate more or less, be more creative than me

    0 讨论(0)
  • 2020-11-21 23:19

    You can use this code, I hope it will be helpful for you.

    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 about Random code generator in PHP

    0 讨论(0)
提交回复
热议问题