How does php generate 6-bit non-repeated strings?

Source: Internet
Author: User
How does php generate 6-bit non-repeated strings? How does php generate 6-bit non-repeated strings?

Reply content:

How does php generate 6-bit non-repeated strings?

Study the timestamp. if you do not repeat it, you only need the timestamp.


  

Md5 of the current time. the first six digits are truncated. Theoretically impossible to repeat

In the early stage of large-scale e-commerce website architecture, architects need about 55% of their time to consider orders. Because most e-commerce website businesses and website logic depend on orders (including the order status) in operation. Now it is so important to analyze how PHP generates high-quality orders (below we will provide code snippets for PHP to generate orders ). I am a little scared to write this article. all of them are personal opinions and are for reference only.

First, understand the general rules for order generation (partial excerpt from the Internet ):

1. do not repeat. 2. security. 2.1 it is not recommended to use sensitive data as order generation rules (for example, user UID, order auto-increment ORDER_ID, etc.) to expose some sensitive information of the website. 3. large-scale random codes cannot be used. Why? 3.1 First, I would like to ask you, "Can random in programming languages be truly random? ", I can tell you with no confidence that at least PHP cannot do it, so it may lead to the first" not repeated "principle. 3.2 if your order quantity reaches times, each time you generate an order code, you have to compare million historical data records. generally, at, you have to spend time optimizing the database (simple processing: Using partitions to create indexes, however, the actual requirements are high and you may need to operate in the primary database. you may think about the pain points. of course, there are other optimization methods for real-time database read/write, which will not be described here ). 4. prevent concurrency. 5. number of control digits. Why? 5.1 ease of query and retrieval 5.2-digit control to 10 ~ 20 digits, of course, 10 ~ 15 The reason is that it is conducive to input. it is good for the user experience that the input is too long (the new copy order button) 6. it is of service significance as much as possible (not mandatory, according to the company's business) 6.1 when your business is relatively large or there is a large-scale expansion in the future, we suggest you consider the significance of placing an order and make adjustments based on your business. The simplest example is: "a marketplace that sells cosmetics, you need to generate a report based on the order source (pc/mobile). at this time, you may need to" 7. solution 7.1 is similar to UUID generation, which does not depend on the external serial number. order numbers generated by time stamps and random numbers cannot avoid conflicts. Therefore, an external serial number generation mechanism must be introduced. You can also use a database or a cache such as APC. There is a problem with caching such as APC, that is, the data cannot be maintained permanently. the serial number counter is cleared when the server is restarted or the PHP host process is restarted, therefore, you can use the cache + database combination mode-if there is a flow number counter data in the cache, read and accumulate the count, if there is no flow number counter in the cache to restore the counter from the database. The counter can be reset at intervals. Since the self-incrementing serial number counter is introduced, it will lead to the "German tank problem" at the beginning of the article, so you need to use the skip32 algorithm to encrypt the serial number ( https://github.com/nlenepveu/Skip32 ). Order Number = Date prefix + encrypted sequential number // Skip32 algorithm encryption key const ENCRYPTED_KEY = 'xxxxxxxxxxxx'; // use Wincache as the serial number counter cache function getOrderSerialNumber () {$ timestamp = time (); $ datePrefix = date ('ymd', $ timestamp); // if the counter data of the serial number is not in the cache, then try to restore if (false = ($ value = wincache_ucache_inc ($ datePrefix) {wincache_lock ($ datePrefix) from the database ); // Obtain the number of orders for today from the database $ counter = getNumberOfOrdersTodayFromDatabase ($ timestamp); $ value = $ c Ounter + 1; if (! Wincache_ucache_add ($ datePrefix, $ value, 60*60*24) {$ value = wincache_ucache_inc ($ datePrefix);} wincache_unlock ($ datePrefix);} return $ datePrefix. str_pad (Skip32: encrypt ($ datePrefix. ENCRYPTED_KEY, $ value), 10, '0', STR_PAD_LEFT );}

The last method used to generate a short URL

/**
* Generate a random number
* @ Param
*/
Function randStr ($ m = 5 ){

$new_str= '';$str= 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwsyz0123456789';for($i= 1; $i<= $m; ++$i) {    $new_str.=$str[mt_rand(0, 61)];}return $new_str;

}

Use the bigint auto-increment field of the database (ensure it is unique) and convert it to 62 hexadecimal to shorten the length:


   

In fact, the only best way is UUID ..

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.