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
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
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; }
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!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 English version
Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.