uTools-Manuals/docs/php/uniqid.html
2019-04-28 19:00:34 +08:00

118 lines
6.2 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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>生成一个唯一ID</title>
</head>
<body class="docs"><div id="layout">
<div id="layout-content"><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> &mdash; <span class="dc-title">生成一个唯一ID</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">string</span> <code class="parameter">$prefix</code><span class="initializer"> = &quot;&quot;</span></span>
[, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$more_entropy</code><span class="initializer"> = false</span></span>
]] ) : <span class="type">string</span></div>
<p class="para rdfs-comment">
获取一个带前缀、基于当前时间微秒数的唯一ID。
</p>
<div class="caution"><strong class="caution">Caution</strong><p class="para">本函数并不会生成安全加密的值,不应用于加密用途。若需要安全加密的值,考虑使用<span class="function"><a href="openssl_random_pseudo_bytes.html" class="function">openssl_random_pseudo_bytes()</a></span></p></div>
<div class="warning"><strong class="warning">Warning</strong>
<p class="para">
此函数不保证返回值的唯一性。
由于绝大多数系统使用 NTP 或者类似服务调整系统的时间,所以系统时间经常发生变化。
此外,进程/线程可能不会返回唯一的 ID。
<code class="parameter">more_entropy</code> 来增加唯一性的概率。
</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">
有用的参数。例如如果在多台主机上可能在同一微秒生成唯一ID。
</p>
<p class="para">
<code class="parameter">prefix</code>为空则返回的字符串长度为13。<code class="parameter">more_entropy</code>
<strong><code>TRUE</code></strong>则返回的字符串长度为23。
</p>
</dd>
<dt>
<code class="parameter">more_entropy</code></dt>
<dd>
<p class="para">
如果设置为 <strong><code>TRUE</code></strong><span class="function"><strong>uniqid()</strong></span> 会在返回的字符串结尾增加额外的熵使用combined linear congruential generator
使得唯一ID更具唯一性。
</p>
</dd>
</dl>
</p>
</div>
<div class="refsect1 returnvalues" id="refsect1-function.uniqid-returnvalues">
<h3 class="title">返回值</h3>
<p class="para">
返回字符串形式的唯一ID。
</p>
<div class="warning"><strong class="warning">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-4552">
<p><strong>Example #1 <span class="function"><strong>uniqid()</strong></span> 例子</strong></p>
<div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*&nbsp;A&nbsp;uniqid,&nbsp;like:&nbsp;4b3403665fea6&nbsp;*/<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"uniqid():&nbsp;%s\r\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">uniqid</span><span style="color: #007700">());<br /><br /></span><span style="color: #FF8000">/*&nbsp;We&nbsp;can&nbsp;also&nbsp;prefix&nbsp;the&nbsp;uniqid,&nbsp;this&nbsp;the&nbsp;same&nbsp;as&nbsp;<br />&nbsp;*&nbsp;doing:<br />&nbsp;*<br />&nbsp;*&nbsp;$uniqid&nbsp;=&nbsp;$prefix&nbsp;.&nbsp;uniqid();<br />&nbsp;*&nbsp;$uniqid&nbsp;=&nbsp;uniqid($prefix);<br />&nbsp;*/<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"uniqid('php_'):&nbsp;%s\r\n"</span><span style="color: #007700">,&nbsp;</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">/*&nbsp;We&nbsp;can&nbsp;also&nbsp;activate&nbsp;the&nbsp;more_entropy&nbsp;parameter,&nbsp;which&nbsp;is&nbsp;<br />&nbsp;*&nbsp;required&nbsp;on&nbsp;some&nbsp;systems,&nbsp;like&nbsp;Cygwin.&nbsp;This&nbsp;makes&nbsp;uniqid()<br />&nbsp;*&nbsp;produce&nbsp;a&nbsp;value&nbsp;like:&nbsp;4b340550242239.64159797<br />&nbsp;*/<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"uniqid('',&nbsp;true):&nbsp;%s\r\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">uniqid</span><span style="color: #007700">(</span><span style="color: #DD0000">''</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>
</pre></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">Note</strong>:
<p class="para">
在Cygwin环境下为了使此函数能够工作<code class="parameter">more_entropy</code> 必须设置为 <strong><code>TRUE</code></strong>
</p>
</p></blockquote>
</div>
</div></div></div></body></html>