Php generates 12-digit, non-repeating, and alphanumeric member card numbers

Source: Internet
Author: User
Tags usleep
Php generates 12-digit, unique-digit, and alphanumeric member card numbers without querying the database. when each member logs in, a unique membership card number is generated.


Reply to discussion (solution)

function generate_password( $length = 12 ) {    $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';    $password = '';    for ( $i = 0; $i < $length; $i++ )     {        $password .= $chars[ mt_rand(0, strlen($chars) - 1) ];    }    return $password;}echo generate_password(12);

$ Alpha_numeric = 'abcdefghijklmnopqrstuvwxyz01234567890 ';
Echo substr (str_shuffle ($ alpha_numeric), 0, 2). strtotime ("now ");

Time stamp + session id hash calculation
Split and combine in the same rule/order

There are many methods, such as calculating a GUID and getting an md5, then sha1 and then intercepting 12 bits...

The most easy to think of is the use of random numbers, but you cannot prove that the two results must be different.
MD5 generates a 32-bit result string and has proved that MD5 has a "collision": two different contents have the same MD5 value.
Similarly, you cannot prove that the MD5 value after truncation is the same as that of the original string.
Therefore, it is safer to use time as a parameter.

function foo() {  $o = $last = '';  do {    $last = $o;    usleep(10);    $t = explode(' ', microtime());    $o = substr(base_convert(strtr($t[0].$t[1].$t[1], '.', ''), 10, 36), 0, 12);  }while($o == $last);  return $o;}
Of course, this generation algorithm also has limitations. A 12-bit 36-digit number can have a maximum of pow (36, 12) states.
When the total volume exceeds pow (36, 12), repetition is inevitable.

The most easy to think of is the use of random numbers, but you cannot prove that the two results must be different.
MD5 generates a 32-bit result string and has proved that MD5 has a "collision": two different contents have the same MD5 value.
Similarly, you cannot prove that the MD5 value after truncation is the same as that of the original string.
Therefore, it is safer to use time as a parameter.

function foo() {  $o = $last = '';  do {    $last = $o;    usleep(10);    $t = explode(' ', microtime());    $o = substr(base_convert(strtr($t[0].$t[1].$t[1], '.', ''), 10, 36), 0, 12);  }while($o == $last);  return $o;}
Of course, this generation algorithm also has limitations. A 12-bit 36-digit number can have a maximum of pow (36, 12) states.
When the total volume exceeds pow (36, 12), repetition is inevitable.
The moderator makes sense! How is the method on the first floor?

Moderator, is this sentence wrong? $ O = substr (base_convert (strtr ($ t [0]. $ t [1]. $ t [1], '. ', ''), 10, 36), 0, 12 );

#1 the random number is used, so there will be no problem in multiple calls during the same operation.
But it cannot be guaranteed that there is no problem when the program runs for multiple times.

$ O = substr (base_convert (strtr ($ t [0]. $ t [1]. $ t [1], '. ', ''), 10, 36), 0, 12 );
No error
$ T = explode ('', microtime ());
The last $ t [0] is the decimal part in practice, and $ t [1] is the positive part of time.
Because the length of the decimal part is different, it cannot be ensured that the result contains 12 digits.
It may be better to extend the length of the fractional part directly.

Program error! Test it!

How is it possible?

Please paste all the error messages
In the other 41 lines, what did you write? Why can't I see it?

Please paste all the error messages
In the other 41 lines, what did you write? Why can't I see it?
Solved! I pasted it directly from you with spaces. $ O = substr (base_convert (strtr ($ t [0]. $ t [1]. $ t [1], '. ', ''), 10, 36), 0, 12 );

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.