<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >uniqid</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PHP 手册" HREF="index.html"><LINK REL="UP" TITLE="Miscellaneous Functions" HREF="ref.misc.html"><LINK REL="PREVIOUS" TITLE="time_sleep_until" HREF="function.time-sleep-until.html"><LINK REL="NEXT" TITLE="unpack" HREF="function.unpack.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP 手册</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.time-sleep-until.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.unpack.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.uniqid" ></A >uniqid</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN126233" ></A ><P > (PHP 3, PHP 4, PHP 5)</P >uniqid -- Generate a unique ID</DIV ><DIV CLASS="refsect1" ><A NAME="AEN126236" ></A ><H2 >说明</H2 >string <B CLASS="methodname" >uniqid</B > ( [string prefix [, bool more_entropy]] )<BR ></BR ><P > Gets a prefixed unique identifier based on the current time in microseconds. </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN126248" ></A ><H2 >参数</H2 ><P > <P ></P ><DIV CLASS="variablelist" ><DL ><DT ><CODE CLASS="parameter" >prefix</CODE ></DT ><DD ><P > 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 > With an empty <CODE CLASS="parameter" >prefix</CODE >, the returned string will be 13 characters long. If <CODE CLASS="parameter" >more_entropy</CODE > is <TT CLASS="constant" ><B >TRUE</B ></TT >, it will be 23 characters. </P ></DD ><DT ><CODE CLASS="parameter" >more_entropy</CODE ></DT ><DD ><P > If set to <TT CLASS="constant" ><B >TRUE</B ></TT >, <B CLASS="function" >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 ></DIV > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN126268" ></A ><H2 >返回值</H2 ><P > Returns the unique identifier, as a string. </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN126271" ></A ><H2 >范例</H2 ><P > 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 > This will create a 32 character identifier (a 128 bit hex number) that is extremely difficult to predict. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN126275" ></A ><P ><B >例 1. <B CLASS="function" >uniqid()</B > Example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#FF8000">// no prefix<br />// works only in PHP 5 and later versions<br /></font><font color="#0000BB">$token </font><font color="#007700">= </font><font color="#0000BB">md5</font><font color="#007700">(</font><font color="#0000BB">uniqid</font><font color="#007700">());<br /><br /></font><font color="#FF8000">// better, difficult to guess<br /></font><font color="#0000BB">$better_token </font><font color="#007700">= </font><font color="#0000BB">md5</font><font color="#007700">(</font><font color="#0000BB">uniqid</font><font color="#007700">(</font><font color="#0000BB">rand</font><font color="#007700">(), </font><font color="#0000BB">true</font><font color="#007700">));<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN126279" ></A ><H2 >更新日志</H2 ><P > <DIV CLASS="informaltable" ><P ></P ><A NAME="AEN126282" ></A ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><THEAD ><TR ><TH >版本</TH ><TH >说明</TH ></TR ></THEAD ><TBODY ><TR ><TD >5.0.0</TD ><TD > The <CODE CLASS="parameter" >prefix</CODE > parameter was made optional. </TD ></TR ><TR ><TD >4.3.1</TD ><TD > The limit of 114 characters long for <CODE CLASS="parameter" >prefix</CODE > was raised. </TD ></TR ></TBODY ></TABLE ><P ></P ></DIV > </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.time-sleep-until.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >起始页</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.unpack.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >time_sleep_until</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.misc.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >unpack</TD ></TR ></TABLE ></DIV ></BODY ></HTML >