MENU

使用php生成唯一ID的4种方法

January 30, 2021 • 原创文章

第一种

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

第二种

<?php 
echo uniqid(); 
echo uniqid(); 
echo uniqid(); 
?>
输出:
4bfd0e375396b
4bfd0e3753981
4bfd0e3753983

第三种

<?php 
echo md5(uniqid()); 
?>
输出:dfbc5c8c6438de075da28b3c8a413fd0

第四种

官方给予的案例

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;
 }
 返回结果类似:E2DFFFB3-571E-6CFC-4B5C-9FEDAAF2EFD7
Last Modified: February 1, 2021
Archives QR Code
QR Code for this page
Tipping QR Code

感谢以下平台提供云加速支持