【问题标题】:Function to generate access token to be used in web service authentication生成用于 Web 服务身份验证的访问令牌的函数
【发布时间】:2013-01-12 23:15:21
【问题描述】:

我需要一个生成访问令牌以唯一标识用户的 php 函数。我将把这个令牌存储在一个名为“token”的额外列中,并将它用于移动应用程序的身份验证。我在网上搜索了它,所有结果都是指 twitter 或 facebook。我的应用中需要它。

我使用 cakePHP 后端和 Android 平台。

非常感谢。

【问题讨论】:

    标签: php cakephp authentication access-token


    【解决方案1】:

    您也可以使用 php 使用 rendomly 生成令牌,如下所示。

    $length = 20;
    
    function generate_password($length)
    {
        if($length>0) 
        {
            $rand_id="";
            for($i=1; $i<=$length; $i++)
            {
                mt_srand((double)microtime() * 1000000);
                $num = mt_rand(1,36);
                $rand_id .= $this->assign_rand_value($num);
            }
        }
        return $rand_id;
    }
    
    function assign_rand_value($num)
    {
        switch($num)
        {
            case "1":
                $rand_value = "a";
            break;
            case "2":
                $rand_value = "b";
            break;
            case "3":
                $rand_value = "c";
            break;
            case "4":
                $rand_value = "d";
            break;
            case "5":
                $rand_value = "e";
            break;
            case "6":
                $rand_value = "f";
            break;
            case "7":
                $rand_value = "g";
            break;
            case "8":
                $rand_value = "h";
            break;
            case "9":
                $rand_value = "i";
            break;
            case "10":
                $rand_value = "j";
            break;
            case "11":
                $rand_value = "k";
            break;
            case "12":
                $rand_value = "l";
            break;
            case "13":
                $rand_value = "m";
            break;
            case "14":
                $rand_value = "n";
            break;
            case "15":
                $rand_value = "o";
            break;
            case "16":
                $rand_value = "p";
            break;
            case "17":
                $rand_value = "q";
            break;
            case "18":
                $rand_value = "r";
            break;
            case "19":
                $rand_value = "s";
            break;
            case "20":
                $rand_value = "t";
            break;
            case "21":
                $rand_value = "u";
            break;
            case "22":
                $rand_value = "v";
            break;
            case "23":
                $rand_value = "w";
            break;
            case "24":
                $rand_value = "x";
            break;
            case "25":
                $rand_value = "y";
            break;
            case "26":
                $rand_value = "z";
            break;
            case "27":
                $rand_value = "0";
            break;
            case "28":
                $rand_value = "1";
            break;
            case "29":
                $rand_value = "2";
            break;
            case "30":
                $rand_value = "3";
            break;
            case "31":
                $rand_value = "4";
            break;
            case "32":
                $rand_value = "5";
            break;
            case "33":
                $rand_value = "6";
            break;
            case "34":
                $rand_value = "7";
            break;
            case "35":
                $rand_value = "8";
            break;
            case "36":
                $rand_value = "9";
            break;
        }
        return $rand_value;
    }
    

    这将生成 20 个字符的随机令牌。

    那你可以这样称呼它

    $link = $this->generate_password(20);
    

    【讨论】:

    • 考虑过一种不同的方法,而不是所有的 switch...case...break?就像有一个标记为$chars="abcde...456789" 的字符串并且只是重复获得substr($chars, strlen($chars)-1, 1)
    【解决方案2】:

    使用 guid :-

    function getGUID(){
        if (function_exists('com_create_guid')){
            return com_create_guid();
        }else{
            mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
            $charid = strtoupper(md5(uniqid(rand(), true)));
            $hyphen = chr(45);// "-"
            $uuid = chr(123)// "{"
                .substr($charid, 0, 8).$hyphen
                .substr($charid, 8, 4).$hyphen
                .substr($charid,12, 4).$hyphen
                .substr($charid,16, 4).$hyphen
                .substr($charid,20,12)
                .chr(125);// "}"
            return $uuid;
        }
    }
    
    $GUID = getGUID();
    echo $GUID;
    

    或参考此链接http://guid.us/GUID/PHP

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-05
      • 2019-04-07
      • 1970-01-01
      相关资源
      最近更新 更多