美文网首页
PHP生成唯一的激活码

PHP生成唯一的激活码

作者: 安晓生 | 来源:发表于2019-10-10 18:13 被阅读0次

方法一

/** 
 * 生成永远唯一的激活码 
 * @return string 
 */  
public function create_guid($namespace = null) {  
    static $guid = '';  
    $uid = uniqid ( "", true );  
      
    $data = $namespace;  
    $data .= $_SERVER ['REQUEST_TIME'];     // 请求那一刻的时间戳  
    $data .= $_SERVER ['HTTP_USER_AGENT'];  // 获取访问者在用什么操作系统  
    $data .= $_SERVER ['SERVER_ADDR'];      // 服务器IP  
    $data .= $_SERVER ['SERVER_PORT'];      // 端口号  
    $data .= $_SERVER ['REMOTE_ADDR'];      // 远程IP  
    $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;  
}  
  
//使用  
echo $key = create_guid ();

方法二:

public  function make_coupon_card() {    
        $code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';    
        $rand = $code[rand(0,25)]    
            .strtoupper(dechex(date('m')))    
            .date('d').substr(time(),-5)    
            .substr(microtime(),2,5)    
            .sprintf('%02d',rand(0,99));    
        for(    
            $a = md5( $rand, true ),    
            $s = '0123456789ABCDEFGHIJKLMNOPQRSTUV',    
            $d = '',    
            $f = 0;    
            $f < 8;    
            $g = ord( $a[ $f ] ),    
            $d .= $s[ ( $g ^ ord( $a[ $f + 8 ] ) ) - $g & 0x1F ],    
            $f++    
        );    
        return $d;    
    }    
    echo make_coupon_card();

方法三:

public function getRandomString($len, $chars=null)
    {
        if (is_null($chars)){
            $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        }  
        mt_srand(10000000*(double)microtime());
        for ($i = 0, $str = '', $lc = strlen($chars)-1; $i < $len; $i++){
            $str .= $chars[mt_rand(0, $lc)];  
        }
        return $str;
    }

方法四:(TP框架的)

/**
     * 生成激活码
     */
    public function create_cdk()
    {
        if (IS_POST) {
            $cdk_num = intval(I('post.cdk_num'));  //生成数量
            if ($cdk_num) {
                for ($i = 0; $i < $cdk_num; $i++) {
                    //查询是否重复
                    //$cdkey=create_randomstr_s();
                    $check = true;
                    while ($check==true) {
                        $cdkey = create_randomstr_s();
                        $result = M('course_cdkey')->where(array('cdkey' => $cdkey))->find();
                        if (empty($result)) {
                            $data['cdkey'] = $cdkey;
                            $check = false;
                        }
                    }
                    $data['validity']=I('post.validity');
      
                    $data['addTime']=time();
                    $result=M('course_cdkey')->add($data);
                }
                if($result){
                    $this->success('操作成功');
                }
            }
        } else {
            $this->display('create_cdk');
        }

    }
/**
 * 生成随机字符串(数字字母小写)
 * @param string $lenth 长度
 * @return string 字符串
 */
function create_randomstr_s($lenth = 6) {

    return random($lenth, '123456789abcdefghijklmnpqrstuvwxyz');

}

方法五:dechex将10进制转换为16进制

microtime返回微秒数

public function create_code()
    {
        $code = 'abcdefghijklmnopqrstuvwxyz';
        $rand = $code[rand(0, 25)]
            .strtoupper(dechex(date('m')))
            . date('d') . substr(time(), -5)
            . substr(microtime(), 2, 5)
            . sprintf('%02d', rand(0, 99));
        for (
            $a = md5($rand, true),
            $s = '0123456789abcdefghijklmnopqrstuvwxyz',
            $d = '',
            $f = 0;
            $f < 5;
            $g = ord($a[$f]),
            $d .= $s[($g ^ ord($a[$f + 8])) - $g & 0x1F],
            $f++
        ) ;
        return $d;
    }

方法六:循环生成激活码

/**
     * Created by PhpStorm.
     * Function:rand_str
     * User: zs
     * Date: 2019/10/12
     * Time: 14:40
     */
    public static function rand_str($len = 8){

        $str = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ0123456789";
        return substr( str_shuffle($str) , 0 , $len  );
    }

    /**生成激活码每次100个。
     * Created by PhpStorm.
     * Function:Code
     * User: zs
     * Date: 2019/10/12
     * Time: 14:48
     */
    public static function Code(){
        $num = 100;
        $codeArr = [];
        for($i=0;$i<$num;$i++){
            $code = self::rand_str(8);
            $codeArr[$i]['code'] = $code;  
              //下面代码是循环存入到数据库了。YII2 框架的
            //$model = Yii::createObject(UserCode::class);
            //$model->setAttributes([
                //'code'               =>$codeArr[$i]['code'],
               // 'created_at'         =>time(),
               // 'generate_author'    =>Yii::$app->user->identity->username,
           // ], false);
          //$model->save();
        }
        return true;
    }

相关文章

网友评论

      本文标题:PHP生成唯一的激活码

      本文链接:https://www.haomeiwen.com/subject/sdpapctx.html