# Better random source for `uniqid()`

> Section in “New and Extended Functionality › Cryptography and Security” of the book [PHP 7 Explained](https://php7explained.com/) by Sebastian Bergmann and Stefan Priebsch, published by thePHP.cc. The book covers PHP 7.0 to 7.4 and was last updated in November 2020. HTML version: <https://php7explained.com/new-and-extended-functionality/cryptography-and-security/better-random-source-for-uniqid/>

Computer programs frequently need unique identifiers. Quite often, an autoincrement index of the database is used. But what if you do not have a database at hand, because you are writing a standalone script?

Even though PHP has a built-in function `uniqid()` to create unique values (or identifiers), you should be aware that they should never be used for cryptography, and are not really guaranteed to be unique. If you require unique identifiers, you should look into the UUID (Universally Unique Identifier) standard. Generating a UUID, however, requires an external library or operating system functionality, and technically there is still no guarantee that there are no collisions. Collisions are extremely unlikely, but technically they can still happen.

Sometimes you just need a quick and easy unique identifier. In that case, you can use `uniqid()`. In PHP 7, the source of randomness that is used internally to generate those unique identifiers has been changed and collisions are now less likely. Still, bear in mind to not use those so-called unique identifiers for cryptography!

---

Previous: [max_depth Option for unserialize()](https://php7explained.com/new-and-extended-functionality/cryptography-and-security/max_depth-option-for-unserialize/index.html.md) | Next: [Improved SSL/TLS](https://php7explained.com/new-and-extended-functionality/cryptography-and-security/improved-ssltls/index.html.md)
