zoukankan      html  css  js  c++  java
  • 如何实现用户id生成一个唯一邀请码

    #如何实现用户id生成一个唯一邀请码
    #创建验证码
    function createCode($user_id) {
    
        static $source_string = 'E5FCDG3HQA4B1NOPIJ2RSTUV67MWX89KLYZ';
    
        $num = $user_id;
    
        $code = '';
    
        while ( $num > 0) {
    
            $mod = $num % 35;
    
            $num = ($num - $mod) / 35;
    
            $code = $source_string[$mod].$code;
    
        }
    
        if(empty($code[3]))
    
            $code = str_pad($code,4,'0',STR_PAD_LEFT);
    
        return $code;
    
    }
    
    #解密验证码
    function decode($code) {
    
        static $source_string = 'E5FCDG3HQA4B1NOPIJ2RSTUV67MWX89KLYZ';
    
        if (strrpos($code, '0') !== false)
    
            $code = substr($code, strrpos($code, '0')+1);
    
        $len = strlen($code);
    
        $code = strrev($code);
    
        $num = 0;
    
        for ($i=0; $i < $len; $i++) {
    
            $num += strpos($source_string, $code[$i]) * pow(35, $i);
    
        }
    
        return $num;
    }
    
    #测试一下
    $number = 18759;
    $encode_code = createCode($number);
    echo $encode_code;#0P4Z
    echo '<br>';
    $decode_code = decode($encode_code);
    echo $decode_code;#18759

    在不知道$source_string的情况下,是没办法解出正确的user_id的

  • 相关阅读:
    POJ 2065 高斯消元求解问题
    HDU1045-Fire Net
    HDU1863-畅通工程
    POJ2524-Ubiquitous Religions
    POJ1064-Cable master
    POJ2456-Aggressive cows
    HDU1272-小希迷宫
    POJ1611-The Suspects
    HDU4496-D-City
    HDU1232-畅通工程
  • 原文地址:https://www.cnblogs.com/lovekingly/p/10294244.html
Copyright © 2011-2022 走看看