php 生成唯一id,网上查了下,有很多的方法
1、md5(time() . mt_rand(1,1000000));
这种方法有一定的概率会出现重复
2、php内置函数uniqid()
uniqid() 函数基于以微秒计的当前时间,生成一个唯一的 ID.
立即学习“PHP免费学习笔记(深入)”;
酷纬企业网站管理系统Kuwebs是酷纬信息开发的为企业网站提供解决方案而开发的营销型网站系统。在线留言模块、常见问题模块、友情链接模块。前台采用DIV+CSS,遵循SEO标准。 1.支持中文、英文两种版本,后台可以在不同的环境下编辑中英文。 3.程序和界面分离,提供通用的PHP标准语法字段供前台调用,可以为不同的页面设置不同的风格。 5.支持google地图生成、自定义标题、自定义关键词、自定义描
1
w3school参考手册有一句话:"由于基于系统时间,通过该函数生成的 ID 不是最佳的。如需生成绝对唯一的 ID,请使用 md5() 函数"。
下面方法返回结果类似:5DDB650F-4389-F4A9-A100-501EF1348872 更多请参考我的博文:php实例
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;
}
}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;
}
以上就是php 生成唯一id的方法详解的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号