Home > Backend Development > PHP Tutorial > How to generate a referral code that is absolutely unique and cannot be easily guessed

How to generate a referral code that is absolutely unique and cannot be easily guessed

WBOY
Release: 2016-08-20 09:04:10
Original
1458 people have browsed it

Do not check in the database

Reply content:

Do not check in the database

<code class="php">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())</code>
Copy after login

For absolutely unique please use UUID
For example, if you are using a linux system

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

After you get a unique string like the one above, you can do whatever you want with it, such as cutting off the length you need.
There are many ways to generate UUID in PHP. The above is just a simple use. I also recommend a class library link


If it is a general recommendation code, it does not need to be complicated unless you have a lot of users. If it is an ordinary company, I think this is enough

<code>echo substr(md5(uniqid(mt_rand(1000, 9999), true)), 20);
// 输出:1c7a6db616de </code>
Copy after login

<code>echo bin2hex(openssl_random_pseudo_bytes(16));
// 输出: 5a8a80e06ffe44fd0a6931707a26f0cc</code>
Copy after login

Similarly, you can also intercept it freely

http://hashids.org/

Just add a verification code

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template