php 生成的唯一安全ID的简单示例

前端之家收集整理的这篇文章主要介绍了php 生成的唯一安全ID的简单示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
PHP生成的唯一安全ID,感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编来看看吧。
经测试代码如下:
  1. /**
  2. * 生成的唯一安全ID
  3. *
  4. * @param
  5. * @arrange (512.笔记) jb51.cc
  6. **/
  7. $u=uuid(); // 0001-7f000001-478c8000-4801-47242987
  8. echo $u;
  9. echo "<br>";
  10. print_r(uuidDecode($u)); // Array ( [serverID] => 0001 [ip] => 127.0.0.1 [unixtime] => 1200390144 [micro] => 0.28126525878906 )
  11. function uuid($serverID=1)
  12. {
  13. $t=explode(" ",microtime());
  14. return sprintf( '%04x-%08s-%08s-%04s-%04x%04x',$serverID,clientIPToHex(),substr("00000000".dechex($t[1]),-8),// get 8HEX of unixtime
  15. substr("0000".dechex(round($t[0]*65536)),-4),// get 4HEX of microtime
  16. mt_rand(0,0xffff),mt_rand(0,0xffff));
  17. }
  18. function uuidDecode($uuid) {
  19. $rez=Array();
  20. $u=explode("-",$uuid);
  21. if(is_array($u)&&count($u)==5) {
  22. $rez=Array(
  23. 'serverID'=>$u[0],'ip'=>clientIPFromHex($u[1]),'unixtime'=>hexdec($u[2]),'micro'=>(hexdec($u[3])/65536)
  24. );
  25. }
  26. return $rez;
  27. }
  28. function clientIPToHex($ip="") {
  29. $hex="";
  30. if($ip=="") $ip=getEnv("REMOTE_ADDR");
  31. $part=explode('.',$ip);
  32. for ($i=0; $i<=count($part)-1; $i++) {
  33. $hex.=substr("0".dechex($part[$i]),-2);
  34. }
  35. return $hex;
  36. }
  37. function clientIPFromHex($hex) {
  38. $ip="";
  39. if(strlen($hex)==8) {
  40. $ip.=hexdec(substr($hex,2)).".";
  41. $ip.=hexdec(substr($hex,2,4,6,2));
  42. }
  43. return $ip;
  44. }
  45. /*** 代码来自编程之家 jb51.cc(jb51.cc) ***/

猜你在找的PHP相关文章