downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Our source is open

The syntax highlighted source is automatically generated by PHP from the plaintext script. If you're interested in what's behind the several functions we used, you can always take a look at the source of the following files:

Of course, if you want to see the source of this page, we have it available. You can also browse the SVN repository for this website on svn.php.net.

Source of: /manual/en/function.uniqid.php

<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once
dirname(__FILE__) ."/toc/ref.misc.inc";
$setup = array (
 
'home' =>
  array (
   
0 => 'index.php',
   
1 => 'PHP Manual',
  ),
 
'head' =>
  array (
   
0 => 'UTF-8',
   
1 => 'en',
  ),
 
'this' =>
  array (
   
0 => 'function.uniqid.php',
   
1 => 'uniqid',
  ),
 
'up' =>
  array (
   
0 => 'ref.misc.php',
   
1 => 'Misc. Functions',
  ),
 
'prev' =>
  array (
   
0 => 'function.time-sleep-until.php',
   
1 => 'time_sleep_until',
  ),
 
'next' =>
  array (
   
0 => 'function.unpack.php',
   
1 => 'unpack',
  ),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);

manual_header();
?>
<div id="function.uniqid" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">uniqid</h1>
  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">uniqid</span> &mdash; <span class="dc-title">Generate a unique ID</span></p>

 </div>
 
 <a name="function.uniqid.description"></a><div class="refsect1 description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">string</span> <span class="methodname"><b>uniqid</b></span>
    ([ <span class="methodparam"><span class="type">string</span> <tt class="parameter">$prefix</tt><span class="initializer"> = &quot;&quot;</span></span>
   [, <span class="methodparam"><span class="type">bool</span> <tt class="parameter">$more_entropy</tt><span class="initializer"> = false</span></span>
  ]] )</div>

  <p class="para rdfs-comment">
   Gets a prefixed unique identifier based on the current time in
   microseconds.
  </p>
 </div>


 <a name="function.uniqid.parameters"></a><div class="refsect1 parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   </p><dl>

    <dt class="varlistentry">

     <span class="term"><i><tt class="parameter">prefix</tt></i>
</span>

     </dt><dd class="listitem">

      <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 <i><tt class="parameter">prefix</tt></i>
, the returned string will
       be 13 characters long.  If <i><tt class="parameter">more_entropy</tt></i>
 is
       <b><tt class="constant">TRUE</tt></b>, it will be 23 characters.
      </p>
     </dd>

   
    <dt class="varlistentry">

     <span class="term"><i><tt class="parameter">more_entropy</tt></i>
</span>

     </dt><dd class="listitem">

      <p class="para">
       If set to <b><tt class="constant">TRUE</tt></b>, <b>uniqid()</b> will add additional
       entropy (using the combined linear congruential generator) at the end
       of the return value, which should make the results more unique.
      </p>
     </dd>

   
   </dl>
<p>
  </p>
 </div>


 <a name="function.uniqid.returnvalues"></a><div class="refsect1 returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns the unique identifier, as a string.
  </p>
 </div>


 <a name="function.uniqid.examples"></a><div class="refsect1 examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   If you need a unique identifier or token and you intend to give
   out that token to the user via the network (i.e. session cookies),
   it is recommended that you use something along these lines:
  </p>
  <p class="para">
   This will create a 32 character identifier (a 128 bit hex number)
   that is extremely difficult to predict.
   </p><div class="example">
    <p><b>Example #1 <b>uniqid()</b> Example</b></p>
    <div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;no&nbsp;prefix<br />//&nbsp;works&nbsp;only&nbsp;in&nbsp;PHP&nbsp;5&nbsp;and&nbsp;later&nbsp;versions<br /></span><span style="color: #0000BB">$token&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">md5</span><span style="color: #007700">(</span><span style="color: #0000BB">uniqid</span><span style="color: #007700">());<br /><br /></span><span style="color: #FF8000">//&nbsp;better,&nbsp;difficult&nbsp;to&nbsp;guess<br /></span><span style="color: #0000BB">$better_token&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">md5</span><span style="color: #007700">(</span><span style="color: #0000BB">uniqid</span><span style="color: #007700">(</span><span style="color: #0000BB">mt_rand</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div><p>
  </p>
 </div>


 <a name="function.uniqid.changelog"></a><div class="refsect1 changelog">
  <h3 class="title">Changelog</h3>
  <p class="para">
   </p><table class="doctable informaltable">
   
     <thead valign="middle">
      <tr valign="middle">
       <th>Version</th>
       <th>Description</th>
      </tr>

     </thead>

     <tbody valign="middle" class="tbody">
      <tr valign="middle">
       <td align="left">5.0.0</td>
       <td align="left">
        The <i><tt class="parameter">prefix</tt></i>
 parameter was made optional.
       </td>
      </tr>

      <tr valign="middle">
       <td align="left">4.3.1</td>
       <td align="left">
        The limit of 114 characters long for <i><tt class="parameter">prefix</tt></i>

        was raised.
       </td>
      </tr>

     </tbody>
   
   </table>
<p>
  </p>
 </div>


 <a name="function.uniqid.notes"></a><div class="refsect1 notes">
  <h3 class="title">Notes</h3>
  <blockquote><p><b class="note">Note</b>:
  
    Under Cygwin, the <i><tt class="parameter">more_entropy</tt></i>
 must be set
    to <b><tt class="constant">TRUE</tt></b> for this function to work.
   <br />
  </p></blockquote>
 </div>


</div><?php manual_footer(); ?>
 
show source | credits | sitemap | contact | advertising | mirror sites