PHP How to generate a 6-bit non-repeating string

Source: Internet
Author: User
Tags apc
PHP How to generate a 6-bit non-repeating string

Reply content:

PHP How to generate a 6-bit non-repeating string

Study the time stamp, do not repeat this only think of time stamp.


  
   

当前时间的md5,截取前6位。理论上不可能重复

In the early days of the large e-commerce site architecture, architects needed about 55% of the time to consider orders. Because most of the e-commerce website business and website Logic relies on orders (including the status of orders) in operation. So it's important that we analyze how PHP generates high-quality orders (the code snippet for PHP-generated orders will be provided below). Writing this article a little frightened, all is personal view, only as a reference.

First understand the approximate rules for the generation of orders (partly excerpts 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 self-increment order_id, etc.), that will expose the site some sensitive information 3, can not use large-scale random code.     Why? 3.1 First ask you "is random in a programming language really random?" ", I can not confidently tell you that at least PHP does not, so may lead to the first" non-repetition "principle occurs 3.2 if your order number reaches 1000w times, you have to compare 1000w history data each time you generate the order code, usually in 500w, you have to spend time on database optimization ( Simple processing: The use of partitioning, indexing, but the actual requirements of high, may need to operate in the main database, you can think how much pain, of course, for the database real-time reading and writing there are other optimization methods, not described here. 4, prevent concurrency.    5. The number of control bits.    Why 5.1 Easy to retrieve 5.2 digits control to 10~20 bit, of course, the Internet will be 10~15 reason is conducive to input, for the input too long user experience Good (new copy order button) 6, as far as possible business significance (not mandatory, according to the company's business) 6.1 when your business is larger Time, or later on a large scale expansion, it is recommended to consider the formation of the order of significance, according to the company's business to make adjustments. For the simplest example: "A shopping mall that sells cosmetics, you need to generate a report based on the source of the order (pc/Mobile), which may require" 7. Workaround 7.1 Similar to generating UUID, independent of external serial number, full timestamp and random number generation order number can not avoid conflict, so must introduce external The serial number generation mechanism of the part. Or use a database, or use a cache such as APC. There is a problem with a cache such as APC, that is, the data cannot be persisted, the server restarts or the PHP host process restarts will empty the serial number counter, so you can take the cache + database combined mode-if the cache has a serial number counter data read and accumulate count, If there is no serial number counter in the cache, restore the counter from the database. The counter can be reset every other time.        Since the introduction of the self-increment serial number counter, will lead to the beginning of the article "German tank problem", so need to use the SKIP32 algorithm to encrypt the serial number (HTTPS://GITHUB.COM/NLENEPVEU/SKIP32).    Order Number = Date prefix + cryptographic serial number//SKIP32 algorithm encryption key const ENCRYPTED_KEY = ' xxxxxxxxxxxx '; Using Wincache as the serial number counter cache   function Getorderserialnumber () {$timestamp = time ();    $datePrefix = Date (' Ymd ', $timestamp); If the serial number counter data is not in the cache, try to recover the if from the database (false = = = ($value = Wincache_ucache_inc ($datePrefix))) {Wincache_lock ($da    Teprefix);        Gets the number of orders today from the database $counter = Getnumberoforderstodayfromdatabase ($timestamp);    $value = $counter + 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), ten, ' 0 ', str_pad_left); }

The last method to generate a short URL

/**
* Generate random numbers
* @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;

}

Using the database bigint self-increment field (guaranteed unique), turn to 62 to shorten the length:


  
    

其实想要唯一最好的办法还是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.