Maison> php教程> PHP开发> le corps du texte

使用PHP uniqid函数生成唯一ID

高洛峰
Libérer: 2016-12-28 15:32:22
original
1853 Les gens l'ont consulté

生成唯一ID的应用场景非常普遍,如临时缓存文件名称,临时变量,临时安全码等,uniqid()函数基于以微秒计的当前时间,生成一个唯一的 ID。由于生成唯一ID与微秒时间关联,因此ID的唯一性非常可靠。

生成的唯一ID默认返回的字符串有 13 个字符串长,如果不定义唯一ID的前缀,最多可返回23个字符串长,如果再结合md5()函数,生成的唯一ID可靠性将更高,这种生成的ID比随机性的ID 最大优点在于可实现排序,特别是一些需要存储在数据库中的值。

一,函数原型

string uniqid ( [string prefix [, bool more_entropy]] )
Copier après la connexion

可定义唯一ID的前缀与长度

二,版本兼容

PHP 3, PHP 4, PHP 5

三,函数基础用法与实例

1,生成一个唯一ID

Copier après la connexion

2,结合md5()函数生成一个唯一ID

Copier après la connexion

输出:dfbc5c8c6438de075da28b3c8a413fd0

3,生成多个唯一ID,由于是以微秒计

Copier après la connexion

输出:

4bfd0e375396b
4bfd0e3753981
4bfd0e3753983

由生成的结果来看,唯一ID之间具有可排序性的。
使用uniqid()函数生成唯一ID既能用于生成临时性ID也能用于生成永久性唯一ID(存储数据库)。

ps:php 生成唯一id的几种解决方法

下面小编给大家整理了三种解决办法,具体内容如下:

1、md5(time() . mt_rand(1,1000000));

  这种方法有一定的概率会出现重复

2、php内置函数uniqid()

  uniqid() 函数基于以微秒计的当前时间,生成一个唯一的 ID.

  w3school参考手册有一句话:"由于基于系统时间,通过该函数生成的 ID 不是最佳的。如需生成绝对唯一的 ID,请使用 md5() 函数"。

  下面方法返回结果类似:5DDB650F-4389-F4A9-A100-501EF1348872

function uuid() { if (function_exists ( 'com_create_guid' )) { return com_create_guid (); } else { mt_srand ( ( double ) microtime () * 10000 ); //optional for php 4.2.0 and up.随便数播种,4.2.0以后不需要了。 $charid = strtoupper ( md5 ( uniqid ( rand (), true ) ) ); //根据当前时间(微秒计)生成唯一id. $hyphen = chr ( 45 ); // "-" $uuid = '' . //chr(123)// "{" substr ( $charid, 0, 8 ) . $hyphen . substr ( $charid, 8, 4 ) . $hyphen . substr ( $charid, 12, 4 ) . $hyphen . substr ( $charid, 16, 4 ) . $hyphen . substr ( $charid, 20, 12 ); //.chr(125);// "}" return $uuid; } }
Copier après la connexion

com_create_guid()是php自带的生成唯一id方法,php5之后貌似已经没有了。

3、官方uniqid()参考手册有用户提供的方法,结果类似:{E2DFFFB3-571E-6CFC-4B5C-9FEDAAF2EFD7}

public function create_guid($namespace = '') { static $guid = ''; $uid = uniqid("", true); $data = $namespace; $data .= $_SERVER['REQUEST_TIME']; $data .= $_SERVER['HTTP_USER_AGENT']; $data .= $_SERVER['LOCAL_ADDR']; $data .= $_SERVER['LOCAL_PORT']; $data .= $_SERVER['REMOTE_ADDR']; $data .= $_SERVER['REMOTE_PORT']; $hash = strtoupper(hash('ripemd128', $uid . $guid . md5($data))); $guid = '{' . substr($hash, 0, 8) . '-' . substr($hash, 8, 4) . '-' . substr($hash, 12, 4) . '-' . substr($hash, 16, 4) . '-' . substr($hash, 20, 12) . '}'; return $guid; }
Copier après la connexion

更多使用PHP uniqid函数生成唯一ID相关文章请关注PHP中文网!

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
    Derniers téléchargements
    Plus>
    effets Web
    Code source du site Web
    Matériel du site Web
    Modèle frontal
    À propos de nous Clause de non-responsabilité Sitemap
    Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!