<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Generate a unique ID</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.time-sleep-until.html">time_sleep_until</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.unpack.html">unpack</a></div> <div class="up"><a href="ref.misc.html">Misc. Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.uniqid" class="refentry"> <div class="refnamediv"> <h1 class="refname">uniqid</h1> <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">uniqid</span> — <span class="dc-title">Generate a unique ID</span></p> </div> <div class="refsect1 description" id="refsect1-function.uniqid-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">string</span> <span class="methodname"><strong>uniqid</strong></span> ([ <span class="methodparam"><span class="type">string</span> <code class="parameter">$prefix</code><span class="initializer"> = ""</span></span> [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$more_entropy</code><span class="initializer"> = <strong><code>FALSE</code></strong></span></span> ]] )</div> <p class="para rdfs-comment"> Gets a prefixed unique identifier based on the current time in microseconds. </p> <div class="caution"><strong class="caution">Caution</strong><p class="para">This function does not generate cryptographically secure values, and should not be used for cryptographic purposes. If you need a cryptographically secure value, consider using <span class="function"><a href="function.random-int.html" class="function">random_int()</a></span>, <span class="function"><a href="function.random-bytes.html" class="function">random_bytes()</a></span>, or <span class="function"><a href="function.openssl-random-pseudo-bytes.html" class="function">openssl_random_pseudo_bytes()</a></span> instead.</p></div> <div class="warning"><strong class="warning">Warning</strong> <p class="para"> This function does not guarantee uniqueness of return value. Since most systems adjust system clock by NTP or like, system time is changed constantly. Therefore, it is possible that this function does not return unique ID for the process/thread. Use <code class="parameter">more_entropy</code> to increase likelihood of uniqueness. </p> </div> </div> <div class="refsect1 parameters" id="refsect1-function.uniqid-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <code class="parameter">prefix</code></dt> <dd> <p class="para"> Can be useful, for instance, if you generate identifiers simultaneously on several hosts that might happen to generate the identifier at the same microsecond. </p> <p class="para"> With an empty <code class="parameter">prefix</code>, the returned string will be 13 characters long. If <code class="parameter">more_entropy</code> is <strong><code>TRUE</code></strong>, it will be 23 characters. </p> </dd> <dt> <code class="parameter">more_entropy</code></dt> <dd> <p class="para"> If set to <strong><code>TRUE</code></strong>, <span class="function"><strong>uniqid()</strong></span> will add additional entropy (using the combined linear congruential generator) at the end of the return value, which increases the likelihood that the result will be unique. </p> </dd> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.uniqid-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns timestamp based unique identifier as a string. </p> <div class="warning"><strong class="warning">Warning</strong> <p class="para"> This function tries to create unique identifier, but it does not guarantee 100% uniqueness of return value. </p> </div> </div> <div class="refsect1 examples" id="refsect1-function.uniqid-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4680"> <p><strong>Example #1 <span class="function"><strong>uniqid()</strong></span> Example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">/* A uniqid, like: 4b3403665fea6 */<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"uniqid(): %s\r\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">uniqid</span><span style="color: #007700">());<br /><br /></span><span style="color: #FF8000">/* We can also prefix the uniqid, this the same as <br /> * doing:<br /> *<br /> * $uniqid = $prefix . uniqid();<br /> * $uniqid = uniqid($prefix);<br /> */<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"uniqid('php_'): %s\r\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">uniqid</span><span style="color: #007700">(</span><span style="color: #DD0000">'php_'</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">/* We can also activate the more_entropy parameter, which is <br /> * required on some systems, like Cygwin. This makes uniqid()<br /> * produce a value like: 4b340550242239.64159797<br /> */<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"uniqid('', true): %s\r\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">uniqid</span><span style="color: #007700">(</span><span style="color: #DD0000">''</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.uniqid-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Under Cygwin, the <code class="parameter">more_entropy</code> must be set to <strong><code>TRUE</code></strong> for this function to work. </p> </p></blockquote> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.time-sleep-until.html">time_sleep_until</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.unpack.html">unpack</a></div> <div class="up"><a href="ref.misc.html">Misc. Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>