Introduction to four methods of generating unique IDs in php

王林
Release: 2023-03-09 12:52:01
forward
19524 people have browsed it

Introduction to four methods of generating unique IDs in php

There are many scenarios where unique IDs are used in work, such as temporary cache files, temporary variables, temporary security codes, etc.

The uniqid() function generates a unique ID based on the current time in microseconds. Since generating a unique ID is tied to a subtle time, the uniqueness of the generated ID is very reliable.

The generated unique ID returns a string that is 13 characters long by default. If combined with the MD5() function, the generated unique ID will be more reliable. This generated ID is the largest than a random ID. The advantage is that sorting can be achieved, especially for some values that need to be stored in the database. Of course, random numbers can also be added here.

The following mainly introduces 4 methods of generating unique IDs:

1, md5(time().mt_rand(1,1000000))

Note: This method has There will be a certain probability of duplication

2. PHP built-in function uniqid()

The uniqid() function generates a unique ID based on the subtle current time.

echo uniqid(); echo uniqid(); echo uniqid(); OUTPUT: 5a4b62dd4aeea 5a4b62dd4aff7 5a4b62dd4b069
Copy after login

There is a sentence in the w3school reference manual: "Because it is based on system time, the ID generated by this function is not optimal. If you need to generate an absolutely unique ID, please use the md5() function."

(Free video tutorial:php video tutorial)

3. Combine the md5() function to generate a unique ID

echo md5(uniqid()); OUTPUT: 0ac3d6e99b7f5290c93d730eaf9d7d94
Copy after login

4. Manually go Processing, the official case

public function create_guid($namespace = '') { static $guid = ''; $uid = uniqid("", true); $data = $namespace; $data .= $_SERVER['REQUEST_TIME']; $data .= $_SERVER['HTTP_USER_AGENT']; $data .= $_SERVER['LOCAL_ADDR']; $data .= $_SERVER['LOCAL_PORT']; $data .= $_SERVER['REMOTE_ADDR']; $data .= $_SERVER['REMOTE_PORT']; $hash = strtoupper(hash('ripemd128', $uid . $guid . md5($data))); $guid = '{' . substr($hash, 0, 8) . '-' . substr($hash, 8, 4) . '-' . substr($hash, 12, 4) . '-' . substr($hash, 16, 4) . '-' . substr($hash, 20, 12) . '}'; return $guid; }
Copy after login

returns similar results: E2DFFFB3-571E-6CFC-4B5C-9FEDAAF2EFD7

Related recommendations:php tutorial

The above is the detailed content of Introduction to four methods of generating unique IDs in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!