Current Location: Home> Latest Articles> How to Use uniqid and mt_rand Functions Together to Generate More Secure Unique IDs?

How to Use uniqid and mt_rand Functions Together to Generate More Secure Unique IDs?

gitbox 2025-07-26

uniqid Function

The uniqid function is one of PHP’s built-in functions, used to generate unique IDs based on the current time. Its syntax is as follows:

<span><span><span class="hljs-title function_ invoke__">uniqid</span></span><span>(</span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$prefix</span></span><span> = </span><span><span class="hljs-string">""</span></span><span>, </span><span><span class="hljs-keyword">bool</span></span><span> </span><span><span class="hljs-variable">$more_entropy</span></span><span> = </span><span><span class="hljs-literal">false</span></span><span>): </span><span><span class="hljs-keyword">string</span></span>
</span></span>
  • $prefix: An optional string parameter that will be used as the prefix of the generated unique ID.

  • $more_entropy: A boolean value deciding whether to append additional random characters to the generated ID to increase its complexity; defaults to false.

uniqid generates IDs based on the current timestamp (microseconds), offering high precision, so the generated ID is unique in most cases. For example, calling uniqid() might produce an ID like 5f5d5e5c8f1a7.

Drawbacks:

Although uniqid generates unique values based on the current time, its security is not very strong. Since timestamps increase sequentially, multiple calls to uniqid at the same time may produce identical IDs. Additionally, because uniqid generates IDs based on the timestamp by default, if an attacker can predict the order of timestamp increments, the generated ID becomes easy to guess.


mt_rand Function

mt_rand is PHP’s pseudo-random number generator. Compared to the rand function, mt_rand is more efficient and produces higher quality random numbers. Its syntax is as follows:

<span><span><span class="hljs-title function_ invoke__">mt_rand</span></span><span>(</span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-variable">$min</span></span><span> = PHP_INT_MIN, </span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-variable">$max</span></span><span> = PHP_INT_MAX): </span><span><span class="hljs-keyword">int</span></span>
</span></span>
  • $min: The minimum value of the generated random number, defaulting to PHP_INT_MIN.

  • $max: The maximum value of the generated random number, defaulting to PHP_INT_MAX.

mt_rand returns a pseudo-random integer within the specified range, which is sufficient for general random number needs.

Drawbacks:

Although mt_rand provides stronger randomness, it is still pseudo-random and not cryptographically secure. If IDs require higher security (e.g., to prevent guessing or collisions), it may not be safe enough.


How to Combine uniqid and mt_rand?

To improve the security of generated unique IDs, you can combine uniqid and mt_rand. This ensures the ID includes randomness on top of the timestamp, increasing the difficulty of collisions.

Combination Method:

<span><span><span class="hljs-function"><span class="hljs-keyword">function</span></span></span><span> </span><span><span class="hljs-title">generateSecureId</span></span><span>(</span><span><span class="hljs-variable">$prefix</span></span><span> = </span><span><span class="hljs-string">""</span></span><span>) {
    </span><span><span class="hljs-comment">// Generate a time-based unique ID</span></span>
    </span><span><span class="hljs-variable">$uniqid</span></span> = <span class="hljs-title function_ invoke__">uniqid</span>($prefix, true);
</span><span><span class="hljs-variable">$randomNumber</span></span> = <span class="hljs-title function_ invoke__">mt_rand</span>(100000, 999999);

</span><span><span class="hljs-comment">// Concatenate the unique ID and the random number</span></span>
</span><span><span class="hljs-keyword">return</span> $uniqid . $randomNumber;

}

Example Output:

<span><span><span class="hljs-variable">$secureId</span></span> = <span class="hljs-title function_ invoke__">generateSecureId</span>("order_");
</span><span><span class="hljs-keyword">echo</span> $secureId;
</span></span>

The output might look like this:

<span>order_60d17e5c5f1b4.829278031116635204001234
</span>

In this approach, the uniqid function generates the time-based unique part, while mt_rand adds a high-quality pseudo-random number. The combined ID is much harder to predict, not only ensuring uniqueness based on the timestamp but also avoiding the issue of generating identical IDs simultaneously.


Why Combine uniqid and mt_rand?

  1. Increased Randomness: IDs generated by uniqid are timestamp-based and somewhat sequential, which can be guessed by attackers. Adding a random number generated by mt_rand increases unpredictability, making IDs harder to guess.

  2. Collision Prevention: Using uniqid alone can lead to collisions if IDs are generated very close in time. Introducing mt_rand adds randomness that effectively reduces collision chances.

  3. Enhanced Security: In scenarios like payment systems or user verification where IDs may involve sensitive data or need protection from guessing, relying solely on uniqid could pose risks. Adding mt_rand makes the IDs more unpredictable and secure.