0

I want to generate a Reference number that users can use to search or save when they want to send it to the agent, or return to a site.

What I have is this -> what I want:

 '45'     ->  REF4903295762
 '421'    ->  REF3425436346
 '1250'   ->  REF4695039476

Adding the 'REF' string is not a issue, but generating even length numbers and/or letters from a ID and having it decodable back is the important bit.

P.S. Something in line with what URL shorteners do.

4

2 回答 2

0

您可以使用我创建的此类/CI_Library。

<?php
class Xref{
    private $key = 24040410;//secret key - change to your own key (int only)[length = 8]
    private $limit = 67000000;//num limit [do not change]

    function encrypt($num){
        if($num > $this->limit){
            return false;
        }else{
            $binaryString = $this->toBinaryString($num);
            $reversedBinaryString = $this->reverseString($binaryString);
            $reversedBinaryInt = bindec($reversedBinaryString);
            $xor = $this->key ^ $reversedBinaryInt;
            return str_pad($xor, 8, '0', STR_PAD_LEFT);
        }
    }
    function decrypt($str){
        $xor = $this->key ^ intval($str);

        return bindec($this->reverseString($this->toBinaryString($xor)));
    }

    private function toBinaryString($num){
        return str_pad(decbin($num), 26, '0', STR_PAD_LEFT);
    }
    private function reverseString($str){
        return strrev($str);
    }
}
?>

只需使用encrypt($ID)转换为“随机”数字,并decrypt($str)取回 ID。

于 2013-01-09T07:00:52.220 回答
0

嗯......通常REF之后的数字ID。但是,如果您希望它是随机的,您可以创建另一个名为“Ref”的列。您创建一个随机数

$string = rand(0, 9999999);

然后你使用 MySQL 来检查它没有被使用,所以就像

$used = true;
while($used)
{
    $string = rand(0, 9999999);
    $query = mysql_query("SELECT * FROM `table` WHERE `REF`='$string'");
    if(!mysql_num_rows($query))
    {
        $used = false;
    }
}

然后在此之后插入它。然后您以后可以通过该列查找。检查这是否是你使用 mysql_num_rows 的方式,我不确定你是否需要在它之前使用 msyql_fetch_array

于 2012-08-08T11:29:48.887 回答