Generate unique random alphanumeric characters that are 7 characters long

前端 未结 15 724
予麋鹿
予麋鹿 2020-12-09 00:32

It need not be meaningful words - more like random password generation, but the catch is - they should be unique. I will be using this for some kind of package / product cod

相关标签:
15条回答
  • 2020-12-09 01:04

    Use Kohana text,

    http://docs.kohanaphp.com/helpers/text

    For example,

       $prod_id = text::random('alpha', 7);
    

    If you don't want use the framework, you can simply copy the code. You will find lots of goodies there.

    0 讨论(0)
  • 2020-12-09 01:04

    Heres a very simple way

    $chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
    $temp_pw = substr( str_shuffle( $chars ), 0, 7 );
    if ( check_unique( $temp_pw ) ) {
        $pw = $temp_pw;
    }
    

    You'll have to implement your own check_unique function. That part should be easy.

    0 讨论(0)
  • 2020-12-09 01:06

    Here's how I would solve this problem:

    Consider that the 7 characters can be one of 26 letters (abc..z), or 10 numbers (01...9). This makes 36 possible characters.

    Each time your application generates a new code, have it increment a global variable. You can turn this unique number into a unique string by using a "Hexatridecimal" converter, and by adding filler characters to make up the rest of the string.

    Take a look at this link. I think this guy had the same problem as you: http://www.codemaxima.com/2010/04/the-hexatridecimal-numbering-system/

    0 讨论(0)
  • 2020-12-09 01:10
    $random = substr(hash('md5',openssl_random_pseudo_bytes(32)),0,7);
    
    0 讨论(0)
  • 2020-12-09 01:12

    Given that you mention passwords here, I'll assume you need a secure method (ie: someone shouldn't be able to guess someone else's password based on knowing any other password). You could use the following:

    1. Decide on a master password, for example "MasterPassword"
    2. For each password generated, append to that either a random or sequential nonce, for example "MasterPassword1", "MasterPassword2".
    3. Perform a cryptographic hash on that (SHA, MD5, etc) and covert the hash to a hexadecimal representation, eg "ce7f181a44a4a5b7e43fe2b9a0b1f0c1".
    4. Truncate that to as many characters as you need - perhaps seven as you indicated: "ce7f181".
    5. Check if that has been assigned before. If not, return that as your password. Otherwise, repeat from 2.

    If security is not an issue, steps 1 and 2 by themselves would be sufficient. If security is an issue, it is vital that no one but yourself knows the value of "MasterPassword".

    0 讨论(0)
  • 2020-12-09 01:12

    Galen's answer allows only one use of each character in the password. Not much information in that string. A simple change though:

    $chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
    $passwordlength = 7;
    for ($x = 1; $x <= $passwordlength; $x++) {
      $charlist .= $chars;
    }
    $temp_pw = substr( str_shuffle( $charlist ), 0, $passwordlength );
    
    0 讨论(0)
提交回复
热议问题