PHP生成唯一的激活码转自

来源:互联网 发布:少儿编程软件 编辑:程序博客网 时间:2024/06/04 20:04

本文http://blog.csdn.net/aoyoo111/article/details/30734251

因为项目中要用到兑换码,所以记录一下、

  1. <?php  
  2. /** 
  3.  * 生成永远唯一的激活码 
  4.  * @return string 
  5.  */  
  6. function create_guid($namespace = null) {  
  7.     static $guid = '';  
  8.     $uid = uniqid ( "", true );  
  9.       
  10.     $data = $namespace;  
  11.     $data .= $_SERVER ['REQUEST_TIME'];     // 请求那一刻的时间戳  
  12.     $data .= $_SERVER ['HTTP_USER_AGENT'];  // 获取访问者在用什么操作系统  
  13.     $data .= $_SERVER ['SERVER_ADDR'];      // 服务器IP  
  14.     $data .= $_SERVER ['SERVER_PORT'];      // 端口号  
  15.     $data .= $_SERVER ['REMOTE_ADDR'];      // 远程IP  
  16.     $data .= $_SERVER ['REMOTE_PORT'];      // 端口信息  
  17.       
  18.     $hash = strtoupper ( hash ( 'ripemd128'$uid . $guid . md5 ( $data ) ) );  
  19.     $guid = '{' . substr ( $hash, 0, 8 ) . '-' . substr ( $hash, 8, 4 ) . '-' . substr ( $hash, 12, 4 ) . '-' . substr ( $hash, 16, 4 ) . '-' . substr ( $hash, 20, 12 ) . '}';  
  20.       
  21.     return $guid;  
  22. }  
  23.   
  24. //使用  
  25. echo $key = create_guid ();  
  26. ?>  


原创粉丝点击