【问题标题】:Generate Random But Unique alphanumerical ID for fault codes为故障代码生成随机但唯一的字母数字 ID
【发布时间】:2016-06-04 07:04:17
【问题描述】:

我希望在我的 Web 应用程序中为故障代码生成一个随机但唯一的 ID,但到目前为止我无法提出正确的解决方案,也不知道从哪里开始

我正在寻找一种可以使用字母和数字的解决方案

故障代码:AF-5754

由于这些相关的错误存储在数据库中,我需要它们是唯一的,数据库中的 id 只是一个数字

有什么方法可以解决这个问题

function randomstring($len)
{
$string = "";
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for($i=0;$i<$len;$i++)
$string.=substr($chars,rand(0,strlen($chars)),1);
return $string;
}

这会返回一个随机字符串,但是通过电话读出它对用户不太友好

【问题讨论】:

    标签: php mysqli


    【解决方案1】:

    为什么不从 sql id 自动生成的唯一代码中生成人类友好的代码?

    <?php
    function getFrom($id) // Get a human-friendly code for unique id from database
    {
    
    $prefixes = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";  // Prefix posibilities
    
    $primaryPrefx  = "A"; // Another prefix to increase id limit
    
    if($id > 259999)
    {
    $primaryPrefx = $prefixes[intval($id / 2600000)];
    $id = $id % 2600000;
    }
    
    $prefix = "";  // Prefix to be used
    $errorCode = "";  // The integer to be added after the prefix
    
    $prefix = $prefixes[intval($id / 100000)]; // get the prefix based on $id ( < 10000 => A , 10000 => B, 20000 => C, 30000 => D etc...) 
    
    
    $id = $id % 100000; // now for the integer part (% returns remainder of division. This gets rid of > 9999 part)
    
    if($id < 10000)
    {
      $errorCode .= "0"; // We need five digits after prefix
    if($id < 1000)
    {
        $errorCode .= "0"; // We need five digits after prefix
    
        if($id < 100)
            {
                $errorCode .= "0"; // We need five digits after prefix
                if($id < 10)
                {
                    $errorCode .= "0" . $id;                
                }
                else
                    $errorCode .= $id;
            }
        else
            $errorCode .= $id;
    
    }
    else
        $errorCode .= $id;
    }
    else
        $errorCode = $id;
    
    return $primaryPrefx . "" . $prefix . "-" . $errorCode;
    }
    
    
     echo getFrom(1568110); // Gives AP-68110
     ?>
    

    对 ID

    还有代码 => id 函数

    function getIdFromCode($code)
    {
       $prefixes = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
       $id = 0;
       $id += strrpos($prefixes, $code[0]) * 260000; // first prefix
       $id += strrpos($prefixes, $code[1]) * 100000; // second prefix
       $id += intval(substr($code, 3)); // AA- is removed
       return $id;
    }
    
    echo getIdFromCode("AP-68110"); // Gives 1568110
    

    【讨论】:

    • 是的,我只是从表中返回 ID 号,但我不确定返回 AA-0232 似乎更安全,但我可能错了。上面的代码可能的最大值是多少
    • 260001 是最大值。再次阅读答案以获得解决方案(几乎是另一个前缀)至于安全隐患。 $prefixes 字符串可以是随机字符串(但每个字符唯一)。你也可以左右 4 个整数来玩弄
    • 当我复制并粘贴代码进行测试时,似乎代码会引发错误
    • PHP 语法检查:解析错误:语法错误,第 33 行代码中出现意外的“}”
    • 奇怪的是它对我来说工作正常。自己复制粘贴了一个,效果很好。你能再试一次吗?请先刷新此页面
    猜你喜欢
    • 2011-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-17
    • 2015-07-29
    • 2011-08-19
    相关资源
    最近更新 更多