Home  >  Article  >  Backend Development  >  How to generate a unique invitation code from user ID

How to generate a unique invitation code from user ID

小云云
小云云Original
2018-03-14 10:43:5117357browse

Generate a unique invitation code corresponding to the user ID, ranging from ‘0-9A-Z’. The focus of this requirement lies in the bold part, that is, the user ID must be deduced based on the invitation code, so that the invitation code does not need to be stored in the database. When the number of users is large, the performance can be greatly improved. .

Wrong idea

Randomly generate a string, and then splice the user ID to the end of the string, but this way the ID is too obvious and easy to be exposed, and if the ID is very long, it will cause The invitation code is very long and is not conducive to user use.

So you can insert the user ID into the generated string and insert an ID number every other character. In this way, the ID is mixed in the string and is not easily exposed. However, the length problem has not been optimized, so the Instead of inserting one id number every other character, insert two id numbers every other character. However, the length doesn't seem to be affected much.

Correct answer

Thinking: Is a decimal number shorter or a hexadecimal number?
It must be that hexadecimal is relatively shorter, so we can directly convert the user ID to 10+26=36 hexadecimal, right? The specific code is as follows:

function createCode($user_id)
{
    static $source_string = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $num = $user_id;
    $code = '';
    while($num)
    {
        $mod = $num % 36; 
        $num = ($num - $mod) / 36;
        $code = $source_string[$mod].$code;
    }
    return $code;
}

The invitation code ensures uniqueness and is not too long. The user ID can also be deduced based on the invitation code. However, the disadvantage is that others can also use the invitation code. To deduct user_id, we need to do some optimization.

Optimization

Remove 0 and use it as a complement symbol. For example, if the invitation code is less than four digits, 0 is added to the high bit, so that the 36-digit system becomes 35-digit system, and then the string The order is disrupted, so that the correct user_id cannot be solved without knowing $source_string.

The code is as follows:

function createCode($user_id) {
    static $source_string = 'E5FCDG3HQA4B1NOPIJ2RSTUV67MWX89KLYZ';
    $num = $user_id;
    $code = '';
    while ( $num > 0) {
        $mod = $num % 35;
        $num = ($num - $mod) / 35;
        $code = $source_string[$mod].$code;
    }
    if(empty($code[3]))
        $code = str_pad($code,4,'0',STR_PAD_LEFT);
    return $code;
}

In this way, the unique invitation code corresponding to user_id is generated, and a decoding function is attached:

function decode($code) {
    static $source_string = 'E5FCDG3HQA4B1NOPIJ2RSTUV67MWX89KLYZ';
    if (strrpos($code, '0') !== false)
        $code = substr($code, strrpos($code, '0')+1);
    $len = strlen($code);
    $code = strrev($code);
    $num = 0;
    for ($i=0; $i < $len; $i++) {
        $num += strpos($source_string, $code[$i]) * pow(35, $i);
    }
    return $num;
}

Related recommendations:

php generates a random unique invitation code/discount code with a fixed length

The above is the detailed content of How to generate a unique invitation code from user ID. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn