Posted By

iTony on 03/18/08


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Nix


Very secure ID


 / Published in: PHP
 

(server_id)-(clientIP)-(unixtime)-(milliseconds)-(random)

I can easily determine which server at which time and who initiate creating of object.

  1. <?php
  2.  
  3. $u=uuid(); // 0001-7f000001-478c8000-4801-47242987
  4. echo $u;
  5. echo "<br>";
  6. print_r(uuidDecode($u)); // Array ( [serverID] => 0001 [ip] => 127.0.0.1 [unixtime] => 1200390144 [micro] => 0.28126525878906 )
  7.  
  8. function uuid($serverID=1)
  9. {
  10. $t=explode(" ",microtime());
  11. return sprintf( '%04x-%08s-%08s-%04s-%04x%04x',
  12. $serverID,
  13. clientIPToHex(),
  14. 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.  
  19. function uuidDecode($uuid) {
  20. $rez=Array();
  21. $u=explode("-",$uuid);
  22. if(is_array($u)&&count($u)==5) {
  23. $rez=Array(
  24. 'serverID'=>$u[0],
  25. 'ip'=>clientIPFromHex($u[1]),
  26. 'unixtime'=>hexdec($u[2]),
  27. 'micro'=>(hexdec($u[3])/65536)
  28. );
  29. }
  30. return $rez;
  31. }
  32.  
  33. function clientIPToHex($ip="") {
  34. $hex="";
  35. if($ip=="") $ip=getEnv("REMOTE_ADDR");
  36. $part=explode('.', $ip);
  37. for ($i=0; $i<=count($part)-1; $i++) {
  38. $hex.=substr("0".dechex($part[$i]),-2);
  39. }
  40. return $hex;
  41. }
  42.  
  43. function clientIPFromHex($hex) {
  44. $ip="";
  45. if(strlen($hex)==8) {
  46. $ip.=hexdec(substr($hex,0,2)).".";
  47. $ip.=hexdec(substr($hex,2,2)).".";
  48. $ip.=hexdec(substr($hex,4,2)).".";
  49. $ip.=hexdec(substr($hex,6,2));
  50. }
  51. return $ip;
  52. }
  53.  
  54. ?>

Report this snippet  

You need to login to post a comment.