<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = 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 => 'zh',
  ),
  'this' => 
  array (
    0 => 'function.uniqid.php',
    1 => 'uniqid',
    2 => '生成基于时间的标识符',
  ),
  'up' => 
  array (
    0 => 'ref.misc.php',
    1 => '杂项 函数',
  ),
  'prev' => 
  array (
    0 => 'function.time-sleep-until.php',
    1 => 'time_sleep_until',
  ),
  'next' => 
  array (
    0 => 'function.unpack.php',
    1 => 'unpack',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    'path' => 'reference/misc/functions/uniqid.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.uniqid" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">uniqid</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">uniqid</span> &mdash; <span class="dc-title">生成基于时间的标识符</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.uniqid-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>uniqid</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$prefix</code><span class="initializer"> = &quot;&quot;</span></span>, <span class="methodparam"><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> <code class="parameter">$more_entropy</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong></span></span>): <span class="type"><a href="language.types.string.php" class="type string">string</a></span></div>

  <p class="para rdfs-comment">
   获取基于当前时间的标识符，精度为微秒，以指定 <code class="parameter">prefix</code> 作为前缀，并可选择附加随机生成的值。
  </p>
  <div class="caution"><strong class="caution">警告</strong>
 <p class="para">
  本函数并不会生成安全加密的值，并且<em>不可</em>用于加密或者要求返回值不可猜测的目的。
 </p>
 <p class="para">
  如果需要加密安全随机，则可以将 <span class="classname"><a href="class.random-engine-secure.php" class="classname">Random\Engine\Secure</a></span> 引擎用于
  <span class="classname"><a href="class.random-randomizer.php" class="classname">Random\Randomizer</a></span>。对于简单的用例，<span class="function"><a href="function.random-int.php" class="function">random_int()</a></span>
  和 <span class="function"><a href="function.random-bytes.php" class="function">random_bytes()</a></span> 函数提供了操作系统的 <abbr title="Cryptographically Secure PseudoRandom Number Generator">CSPRNG</abbr> 支持的方便且安全的 <abbr title="Application Programming Interface">API</abbr>。
 </p>
</div>
  <div class="warning"><strong class="warning">警告</strong>
    <p class="para">
     该函数不保证返回值的唯一性，因为该值基于当前时间（以微秒为单位）或当前时间加上少量随机数据（如果
     <code class="parameter">more_entropy</code> 为 <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>）。
    </p>
  </div>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.uniqid-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">prefix</code></dt>
     <dd>
      <p class="para">
       有用的参数。例如同一微秒时在多台主机上同时生成标识符。（如果系统时钟向后移动，例如通过 NTP 调整，即使在单个主机上也会发生这种情况。）
      </p>
      <p class="para">
       <code class="parameter">prefix</code>为空，则返回的字符串长度为
       13。<code class="parameter">more_entropy</code> 为 <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>，则返回的字符串长度为 23。
      </p>
     </dd>
    
    
     <dt><code class="parameter">more_entropy</code></dt>
     <dd>
      <p class="para">
       如果设置为 <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>，<span class="function"><strong>uniqid()</strong></span>
       会在返回的字符串结尾增加额外的熵（使用线性同余组合发生器）。
       使得唯一ID更具唯一性。
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.uniqid-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回字符串形式的，基于时间戳的标识符。
  </p>
  <div class="warning"><strong class="warning">警告</strong>
   <p class="para">
    该函数不保证返回值的唯一性。
   </p>
  </div>
 </div>


 <div class="refsect1 examples" id="refsect1-function.uniqid-examples">
  <h3 class="title">示例</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>示例 #1 <span class="function"><strong>uniqid()</strong></span> 示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/* 一个 uniqid，像：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">/* 也可以为 uniqid 添加前缀，以下两种方式相同：<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">/* 还可以启用 more_entropy 参数，在 <br /> * 某些系统上是必须的，比如 Cygwin。这使得 uniqid()<br /> * 产生如下值：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">?&gt;</span></span></code></div>
    </div>

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


 <div class="refsect1 notes" id="refsect1-function.uniqid-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    在 Cygwin 环境下，为了使此函数能够工作，<code class="parameter">more_entropy</code> 必须设置为 <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>。
   </p>
  </p></blockquote>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.uniqid-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li><span class="function"><a href="function.random-bytes.php" class="function" rel="rdfs-seeAlso">random_bytes()</a> - Get cryptographically secure random bytes</span></li>
  </ul>
 </div>

</div><?php manual_footer($setup); ?>