function.uniqid.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 162 行
HTML
162 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Generate a unique ID</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div 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)</p><p class="refpurpose"><span class="refname">uniqid</span> — <span class="dc-title">Generate a unique ID</span></p> </div> <div class="refsect1 description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">string</span> <span class="methodname"><b><b>uniqid</b></b></span> ([ <span class="methodparam"><span class="type">string</span> <tt class="parameter">$prefix</tt></span> [, <span class="methodparam"><span class="type">bool</span> <tt class="parameter">$more_entropy</tt></span> ]] )</div> <p class="para rdfs-comment"> Gets a prefixed unique identifier based on the current time in microseconds. </p> </div> <div class="refsect1 parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><i><tt class="parameter">prefix</tt></i></span> <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 <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>TRUE</tt></b>, it will be 23 characters. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">more_entropy</tt></i></span> <dd> <p class="para"> If set to <b><tt>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> </dt> </dl> </p> </div> <div class="refsect1 returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the unique identifier, as a string. </p> </div> <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. <div class="example"> <p><b>Example #1 <b>uniqid()</b> Example</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// no prefix<br />// works only in PHP 5 and later versions<br /></span><span style="color: #0000BB">$token </span><span style="color: #007700">= </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">// better, difficult to guess<br /></span><span style="color: #0000BB">$better_token </span><span style="color: #007700">= </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">rand</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 changelog"> <h3 class="title">ChangeLog</h3> <p class="para"> <table class="informaltable"> <colgroup> <thead valign="middle"> <tr valign="middle"> <th colspan="1">Version</th> <th colspan="1">Description</th> </tr> </thead> <tbody valign="middle" class="tbody"> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">5.0.0</td> <td colspan="1" rowspan="1" align="left"> The <i><tt class="parameter">prefix</tt></i> parameter was made optional. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">4.3.1</td> <td colspan="1" rowspan="1" align="left"> The limit of 114 characters long for <i><tt class="parameter">prefix</tt></i> was raised. </td> </tr> </tbody> </colgroup> </table> </p> </div></div><hr /><div 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>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?