php 生成的唯一安全ID的解决办法
内容摘要
这篇文章主要为大家详细介绍了php 生成的唯一安全ID的简单示例,具有一定的参考价值,可以用来参考一下。
php生成的唯一安全ID,对此感兴趣的朋友,看看idc笔记做的技术笔记。经测
php生成的唯一安全ID,对此感兴趣的朋友,看看idc笔记做的技术笔记。经测
文章正文
这篇文章主要为大家详细介绍了php 生成的唯一安全ID的简单示例,具有一定的参考价值,可以用来参考一下。
php生成的唯一安全ID,对此感兴趣的朋友,看看idc笔记做的技术笔记。经测试代码如下:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | <code class = "php" > /** * 生成的唯一安全ID * * @param * @arrange (www.idcnote.com) **/ $u =uuid(); // 0001-7f000001-478c8000-4801-47242987 echo $u ; echo "<br>" ; print_r(uuidDecode( $u )); // Array ( [serverID] => 0001 [ip] => 127.0.0.1 [unixtime] => 1200390144 [micro] => 0.28126525878906 ) function uuid( $serverID =1) { $t = explode ( " " ,microtime()); return sprintf( '%04x-%08s-%08s-%04s-%04x%04x' , $serverID , clientIPToHex(), substr ( "00000000" . dechex ( $t [1]),-8), // get 8HEX of unixtime substr ( "0000" . dechex ( round ( $t [0]*65536)),-4), // get 4HEX of microtime mt_rand(0,0xffff), mt_rand(0,0xffff)); } function uuidDecode( $uuid ) { $rez =Array(); $u = explode ( "-" , $uuid ); if ( is_array ( $u )&& count ( $u )==5) { $rez =Array( 'serverID' => $u [0], 'ip' =>clientIPFromHex( $u [1]), 'unixtime' =>hexdec( $u [2]), 'micro' =>(hexdec( $u [3])/65536) ); } return $rez ; } function clientIPToHex( $ip = "" ) { $hex = "" ; if ( $ip == "" ) $ip = getEnv ( "REMOTE_ADDR" ); $part = explode ( '.' , $ip ); for ( $i =0; $i <= count ( $part )-1; $i ++) { $hex .= substr ( "0" . dechex ( $part [ $i ]),-2); } return $hex ; } function clientIPFromHex( $hex ) { $ip = "" ; if ( strlen ( $hex )==8) { $ip .=hexdec( substr ( $hex ,0,2)). "." ; $ip .=hexdec( substr ( $hex ,2,2)). "." ; $ip .=hexdec( substr ( $hex ,4,2)). "." ; $ip .=hexdec( substr ( $hex ,6,2)); } return $ip ; } /*** 代码来自php教程(www.idcnote.com) ***/ </code> |
注:关于php 生成的唯一安全ID的简单示例的内容就先介绍到这里,更多相关文章的可以留意
代码注释