In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to generate 6-digit non-repetitive strings by php". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Php generates 6-bit non-repeating string methods: 1, through the "substr (md5 (microtime (true)), 0,6);" intercept the first 6 bits; 2, use the database bigint self-increment field, converted to 62 to shorten the length.
This article operating environment: windows7 system, PHP7.1 version, DELL G3 computer
How does php generate 6-bit non-repeating strings?
PHP generates a 6-bit non-repeating string (similar to UUID)
We have to consider:
1. Do not repeat.
2. Security.
[it is not recommended to use sensitive data as order generation rules (for example, user UID, order _ ID, etc.), thinking that some sensitive information on the website will be exposed]
3. Large-scale random codes cannot be used.
Why?
First of all, I ask you, "can random in a programming language be really random?" I can not confidently tell you that at least PHP can't do it, so it may lead to the first "no repetition" principle.
If the number of your order has reached 1000W, you will have to compare 1000W pieces of historical data each time you generate the order code, usually in 500w, you have to spend time on database optimization (simple processing: using partitioning, indexing, but high practical requirements, may need to operate under the main database, but think how painful you are, of course, there are other optimization methods for real-time database reading and writing, which are not introduced here).
4. Prevent concurrency.
5. Number of control digits.
Why?
Easy to query and retrieve
The number of digits can be controlled to 10-20 digits, of course, the online will be 10-15 because it is conducive to input, for the input is too long to do a good user experience (new copy order button)
6. Try to be business meaningful (not compulsory, according to the company's business)
When your business is relatively large, or when you have a large-scale expansion in the later stage, it is recommended to consider the significance of placing an order and make adjustments according to the company's business. To take the simplest example: "in a cosmetics mall, you need to generate reports according to the source of the order (pc/ Mobile), which may be needed at this time"]
7. Solution.
/ / similar to generating UUID, it does not rely on external serial numbers, and conflicts cannot be avoided by generating order numbers entirely by timestamps and random numbers. / / therefore, an external serial number generation mechanism must be introduced. Either use a database or use a cache such as APC. / / there is a problem with using caches such as APC, that is, data cannot be persisted. Server restart or PHP host process restart will clear the serial number counter. / / so you can adopt the mode of cache + database combination-if there is a serial number counter data in the cache, read and accumulate the counter. If there is no serial number counter in the cache, restore the counter from the database. / / the counter can be reset at regular intervals. Now that the self-increasing 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 + encryption pipeline number / / Skip32 algorithm encryption key const ENCRYPTED_KEY = 'xxxxxxxxxxxx';// uses Wincache as the pipeline number counter cache function getOrderSerialNumber () {$timestamp = time (); $datePrefix = date (' ymd', $timestamp) / / if the serial counter data is not in the cache, try to restore if (false = = ($value = wincache_ucache_inc ($datePrefix)) {wincache_lock ($datePrefix)) {wincache_lock ($datePrefix) from the database; / / get today's orders from the database $counter = getNumberOfOrdersTodayFromDatabase ($timestamp); $value = $counter + 1 if (! wincache_ucache_add ($datePrefix, $value, 60 orders) {$value = wincache_ucache_inc ($datePrefix) } wincache_unlock ($datePrefix);} return $datePrefix.str_pad (Skip32::encrypt ($datePrefix.ENCRYPTED_KEY, $value), 10, '0mm, STR_PAD_LEFT);}
Simple way:
1. Current timestamp md5 encryption, intercepting the first 6 bits
Echo substr (md5 (microtime (true)), 0,6)
two。 Use the database bigint self-increment field to convert to 62 decimal to shorten the length
Echo gmp_strval (gmp_init ('9876543210, 10), 62); "how php generates 6-digit non-repeating strings" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.