How to generate a recommendation code that is absolutely unique and not easily guessed

Source: Internet
Author: User
Don't go in the database check

Reply content:

Don't go in the database check

function makeCode($t) {  $time = $t.mt_rand(10,99); // 要是觉得不够可以加上线程ID等。  $ce = $time[9];  $xy = array();  $xy[0] = array('A','F','D','X','G','H','K','U','P','Y','W','P');  $xy[1] = array('R','G','H','P','X','D','Y','U','K','F','S','Z');  // ... 不重复的序列即可  $xy[9] = array('S','K','T','U','E','Q','V','G','R','X','C','J');  $rb = '';  for ($i = 12;$i--;) {    $rb .= $xy[$ce][$time[$i]];  }  return $rb;}makecode(time())

For 绝对唯一 speaking please useUUID
For example, you are using a Linux system that can

echo file_get_contents('/proc/sys/kernel/random/uuid');// 输出: 6c509fc7-328b-4d82-9af8-60c44e7b84b4

When you get a unique string like this, you can do whatever you want to do with it, such as intercepting the length you need.
PHP generates UUID in a number of ways, it is simple to use, also recommended a class library link

If it is a general recommendation code actually does not need to make very complicated, unless you have a lot of user volume. If it's a general company, I think it's enough.

echo bin2hex(openssl_random_pseudo_bytes(16));// 输出: 5a8a80e06ffe44fd0a6931707a26f0cc

Likewise, you are free to intercept

http://hashids.org/

Add a check code to it.

  • 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.