The online search results are similar to the following: {code...} read the UUID instructions. how do I feel that the hit rate of these generation methods is below the standard? The search results are similar to the following:
$chars = md5(uniqid(mt_rand(), true)); $uuid = substr($chars,0,8) . '-'; $uuid .= substr($chars,8,4) . '-'; $uuid .= substr($chars,12,4) . '-'; $uuid .= substr($chars,16,4) . '-'; $uuid .= substr($chars,20,12); return $prefix . $uuid;
Mt_srand (double) microtime () * 10000); // optional for php 4.2.0 and up. no need to sow it after 4.2.0. $ Charid = strtoupper (md5 (uniqid (rand (), true); // generate a unique id based on the current time (in microseconds. $ hyphen = chr (45); $ uuid = ''. substr ($ charid, 0, 8 ). $ hyphen. substr ($ charid, 8, 4 ). $ hyphen. substr ($ charid, 12, 4 ). $ hyphen. substr ($ charid, 16, 4 ). $ hyphen. substr ($ charid, 20, 12); return $ uuid;
After reading the UUID instructions, how do I feel that the impact rate of these generation methods does not reach the standard?
Reply content:
The search results are similar to the following:
$chars = md5(uniqid(mt_rand(), true)); $uuid = substr($chars,0,8) . '-'; $uuid .= substr($chars,8,4) . '-'; $uuid .= substr($chars,12,4) . '-'; $uuid .= substr($chars,16,4) . '-'; $uuid .= substr($chars,20,12); return $prefix . $uuid;
Mt_srand (double) microtime () * 10000); // optional for php 4.2.0 and up. no need to sow it after 4.2.0. $ Charid = strtoupper (md5 (uniqid (rand (), true); // generate a unique id based on the current time (in microseconds. $ hyphen = chr (45); $ uuid = ''. substr ($ charid, 0, 8 ). $ hyphen. substr ($ charid, 8, 4 ). $ hyphen. substr ($ charid, 12, 4 ). $ hyphen. substr ($ charid, 16, 4 ). $ hyphen. substr ($ charid, 20, 12); return $ uuid;
After reading the UUID instructions, how do I feel that the impact rate of these generation methods does not reach the standard?
At present, many so-called php uuid generation methods are based on random numbers, which is difficult to duplicate in practice, but theoretically it is obviously not feasible.
Here we use MongoDb ObjectId as an example. I personally think this generation method can be introduced into PHP.
ObjectId consists of four parts: timestamp, millisecond count, machine code, and auto-increment count.
You don't need to elaborate on the first two. you can obtain both PHP and focus on the latter two.
The machine code is mainly used to avoid generating the same UUID between different hosts.
Currently, the main generation method of machine code is to calculate hash based on hardware information, which is not applicable to PHP.
The main reason is that PHP cannot directly obtain hardware information (by means of extension or local command execution), and PHP requests and requests cannot share data, therefore, each request must pull the machine code, which has a great impact on performance.
There is a very simple way to solve this problem, that is, calculate the machine code on different machines, then directly write the configuration file, and directly read the code when PHP is processing.
Auto-increment count is mainly used to avoid generating the same UUID in concurrent processing.
For auto-increment, PHP cannot directly implement auto-increment counting because it cannot share data between requests.
To solve this problem, we can introduce two extensions of PHP, Semaphore and Shared Memory, which are both integrated in the PHP source code, you can enable it through -- enable-sysvsem and -- enable-sysvshm. We only need to place the auto-increment in the shared memory, and then use the semaphore to restrict access to achieve the purpose of sharing the auto-increment technology for concurrent requests. Of course, auto-increment can also be implemented using other tools.
The Linux kernel provides the UUID generation interface:
cat /proc/sys/kernel/random/uuid
Everything in Linux is a file. no matter what program, you can obtain a UUID by reading the file.
Generate a unique value:
Php best. php
In addition, it seems to be usedopenssl_random_pseudo_bytesA good random string can also be generated:
echo base64_encode(openssl_random_pseudo_bytes(32));