How to implement a user ID generate a unique invitation code

Source: Internet
Author: User
根据用户id生成与之对应的唯一邀请码,范围为‘0-9A-Z’。The focus of this demand is the bold part, that is, to be able to reverse the introduction of the user ID according to the invitation code, so that the invitation code will not be put into storage, in the case of a large number of users, performance can be greatly improved.

Wrong ideas

Randomly generate a string, and then stitching the user ID behind the string, but so the ID is too obvious, easy to expose, and if the ID is very long, it will cause the invitation code is very long, not conducive to user use.

So you can insert the user ID into the generated string, insert an ID number in a single character, so that the ID is mixed in the string, it is not easy to expose, but the length problem is not optimized, so the number of characters inserted in one ID is changed to insert a number of two IDs each character. However, the length does not seem to be affected too much.

Positive solution

Think: is a 10-digit short or a 16-binary digit short?
Must be 16 binary relatively short, so we can directly convert the user ID into the 10+26=36 of the system is not OK? The specific code is as follows:

function Createcode ($user _id) {    static $source _string = ' 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ ';    $num = $user _id;    $code = ";    while ($num)    {        $mod = $num%;         $num = ($num-$mod)/;        $code = $source _string[$mod]. $code;    }    return $code;}

The invitation code guarantees the uniqueness, and the length is not too long, the user ID can also be launched according to the invitation code, but one thing is bad, others can also according to the invitation code to Anti-roll user_id, so we need to do some optimization.

Optimization

The 0 culling, as a complement symbol, such as less than four-bit invitation code in the high 0, so that 36 into 35 into the system, and then the string order is scrambled, so that, in the case of not knowing $source_string, there is no way to solve the correct user_id.

The code is as follows:

function Createcode ($user _id) {    static $source _string = ' E5fcdg3hqa4b1nopij2rstuv67mwx89klyz ';    $num = $user _id;    $code = ";    while ($num > 0) {        $mod = $num%;        $num = ($num-$mod)/;        $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 the 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 ($i); } return $num;} 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.