当前位置:Gxlcms > PHP教程 > php唯一查询码怎么生成

php唯一查询码怎么生成

时间:2021-07-01 10:21:17 帮助过:31人阅读

php生成唯一唯一查询码的方法实例

php 生成唯一id,有很多的方法。

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

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

2、php内置函数uniqid()

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

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

com_create_guid()是php自带的生成唯一id方法,php5之后貌似已经没有了。
3、官方uniqid()参考手册有用户提供的方法,结果类似:{E2DFFFB3-571E-6CFC-4B5C-9FEDAAF2EFD7}

  1. public function create_guid($namespace = '') {
  2. static $guid = '';
  3. $uid = uniqid("", true);
  4. $data = $namespace;
  5. $data .= $_SERVER['REQUEST_TIME'];
  6. $data .= $_SERVER['HTTP_USER_AGENT'];
  7. $data .= $_SERVER['LOCAL_ADDR'];
  8. $data .= $_SERVER['LOCAL_PORT'];
  9. $data .= $_SERVER['REMOTE_ADDR'];
  10. $data .= $_SERVER['REMOTE_PORT'];
  11. $hash = strtoupper(hash('ripemd128', $uid . $guid . md5($data)));
  12. $guid = '{' .
  13. substr($hash, 0, 8) .
  14. '-' .
  15. substr($hash, 8, 4) .
  16. '-' .
  17. substr($hash, 12, 4) .
  18. '-' .
  19. substr($hash, 16, 4) .
  20. '-' .
  21. substr($hash, 20, 12) .
  22. '}';
  23. return $guid;
  24. }

推荐教程:PHP视频教程

以上就是php唯一查询码怎么生成的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行