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

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

    This is a simple function that allows you to generate random strings containing Letters and Numbers (alphanumeric). You can also limit the string length. These random strings can be used for various purposes, including: Referral Code, Promotional Code, Coupon Code. Function relies on following PHP functions: base_convert, sha1, uniqid, mt_rand

    function random_code($length)
    {
      return substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, $length);
    }
    
    echo random_code(6);
    
    /*sample output
    * a7d9e8
    * 3klo93
    */
    
    0 讨论(0)
  • 2020-11-21 22:55
    <?php
    function generateRandomString($length = 11) {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $charactersLength = strlen($characters);
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, $charactersLength - 1)];
        }
        return $randomString;
    
    }
    
    ?>
    

    above function will generate you a random string which is length of 11 characters.

    0 讨论(0)
  • 2020-11-21 22:57

    A simple solution is to convert base 64 to alphanumeric by discarding the non-alphanumeric characters.

    This one uses random_bytes() for a cryptographically secure result.

    function random_alphanumeric(int $length): string
    {
        $result='';
        do
        {
            //Base 64 produces 4 characters for each 3 bytes, so most times this will give enough bytes in a single pass
            $bytes=random_bytes(($length+3-strlen($result))*2);
            //Discard non-alhpanumeric characters
            $result.=str_replace(['/','+','='],['','',''],base64_encode($bytes));
            //Keep adding characters until the string is long enough
            //Add a few extra because the last 2 or 3 characters of a base 64 string tend to be less diverse
        }while(strlen($result)<$length+3);
        return substr($result,0,$length);
    }
    
    0 讨论(0)
  • 2020-11-21 22:58

    Security Notice: This solution should not be used in situations where the quality of your randomness can affect the security of an application. In particular, rand() and uniqid() are not cryptographically secure random number generators. See Scott's answer for a secure alternative.

    If you do not need it to be absolutely unique over time:

    md5(uniqid(rand(), true))

    Otherwise (given you have already determined a unique login for your user):

    md5(uniqid($your_user_login, true))
    
    0 讨论(0)
  • 2020-11-21 22:58

    I like to use hash keys when dealing verification links. I would recommend using the microtime and hashing that using MD5 since there should be no reason why the keys should be the same since it hashes based off of the microtime.

    1. $key = md5(rand());
    2. $key = md5(microtime());
    0 讨论(0)
  • 2020-11-21 23:01

    I always use this my function to generate a custom random alphanumeric string... Hope this help.

    <?php
      function random_alphanumeric($length) {
        $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ12345689';
        $my_string = '';
        for ($i = 0; $i < $length; $i++) {
          $pos = mt_rand(0, strlen($chars) -1);
          $my_string .= substr($chars, $pos, 1);
        }
        return $my_string;
      }
      echo random_alphanumeric(50); // 50 characters
    ?>
    

    It generates for example: Y1FypdjVbFCFK6Gh9FDJpe6dciwJEfV6MQGpJqAfuijaYSZ86g

    If you want compare with another string to be sure that is a unique sequence you can use this trick...

    $string_1 = random_alphanumeric(50);
    $string_2 = random_alphanumeric(50);
    while ($string_1 == $string_2) {
      $string_1 = random_alphanumeric(50);
      $string_2 = random_alphanumeric(50);
      if ($string_1 != $string_2) {
         break;
      }
    }
    echo $string_1;
    echo "<br>\n";
    echo $string_2;
    

    it generate two unique strings:

    qsBDs4JOoVRfFxyLAOGECYIsWvpcpMzAO9pypwxsqPKeAmYLOi

    Ti3kE1WfGgTNxQVXtbNNbhhvvapnaUfGMVJecHkUjHbuCb85pF

    Hope this is what you are looking for...

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