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

后端 未结 28 2104
隐瞒了意图╮
隐瞒了意图╮ 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:12

    This function will generate a random key using numbers and letters:

    function random_string($length) {
        $key = '';
        $keys = array_merge(range(0, 9), range('a', 'z'));
    
        for ($i = 0; $i < $length; $i++) {
            $key .= $keys[array_rand($keys)];
        }
    
        return $key;
    }
    
    echo random_string(50);
    

    Example output:

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

    You can use UUID(Universally Unique Identifier), it can be used for any purpose, from user authentication string to payment transaction id.

    A UUID is a 16-octet (128-bit) number. In its canonical form, a UUID is represented by 32 hexadecimal digits, displayed in five groups separated by hyphens, in the form 8-4-4-4-12 for a total of 36 characters (32 alphanumeric characters and four hyphens).

    function generate_uuid() {
        return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
            mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
            mt_rand( 0, 0xffff ),
            mt_rand( 0, 0x0C2f ) | 0x4000,
            mt_rand( 0, 0x3fff ) | 0x8000,
            mt_rand( 0, 0x2Aff ), mt_rand( 0, 0xffD3 ), mt_rand( 0, 0xff4B )
        );
    
    }
    

    //calling funtion

    $transationID = generate_uuid();
    

    some example outputs will be like:

    E302D66D-87E3-4450-8CB6-17531895BF14
    22D288BC-7289-442B-BEEA-286777D559F2
    51B4DE29-3B71-4FD2-9E6C-071703E1FF31
    3777C8C6-9FF5-4C78-AAA2-08A47F555E81
    54B91C72-2CF4-4501-A6E9-02A60DCBAE4C
    60F75C7C-1AE3-417B-82C8-14D456542CD7
    8DE0168D-01D3-4502-9E59-10D665CEBCB2
    

    hope it helps someone in future :)

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

    For really random strings, you can use

    <?php
    
    echo md5(microtime(true).mt_Rand());
    

    outputs :

    40a29479ec808ad4bcff288a48a25d5c
    

    so even if you try to generate string multiple times at exact same time, you will get different output.

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

    If you want to generate a unique string in PHP, try following.

    md5(uniqid().mt_rand());
    

    In this,

    uniqid() - It will generate unique string. This function returns timestamp based unique identifier as a string.

    mt_rand() - Generate random number.

    md5() - It will generate the hash string.

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

    Scott, yes you are very write and good solution! Thanks.

    I am also required to generate unique API token for my each user. Following is my approach, i used user information (Userid and Username):

    public function generateUniqueToken($userid, $username){
    
            $rand = mt_rand(100,999);
        $md5 = md5($userid.'!(&^ 532567_465 ///'.$username);
    
        $md53 = substr($md5,0,3);
        $md5_remaining = substr($md5,3);
    
        $md5 = $md53. $rand. $userid. $md5_remaining;
    
        return $md5;
    }
    

    Please have a look and let me know if any improvement i can do. Thanks

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

    we can use this two line of code to generate unique string have tested around 10000000 times of iteration

      $sffledStr= str_shuffle('abscdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()_-+');
        $uniqueString = md5(time().$sffledStr);
    
    0 讨论(0)
提交回复
热议问题