Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 生成唯一ID序列_Php - Fatal编程技术网

Php 生成唯一ID序列

Php 生成唯一ID序列,php,Php,我想生成一个7号唯一ID,这是我要生成的函数 function generate_guid($length){ $alphabet = '1234567890'; $tip = array(); $alphaLength = strlen($alphabet) - 1; for ($i = 0; $i < $length; $i ++) { $n = rand(0, $alphaLength); $tip[] = $alpha

我想生成一个7号唯一ID,这是我要生成的函数

function generate_guid($length){
    $alphabet = '1234567890';
    $tip = array();
    $alphaLength = strlen($alphabet) - 1;
    for ($i = 0; $i < $length; $i ++) {
        $n = rand(0, $alphaLength);
        $tip[] = $alphabet[$n];
    }
    return implode($tip);
}
现在,我能够生成并检查用户名和电子邮件是否存在。我的问题是如何检查guid是否存在且没有错误,并生成新的guid以插入数据库?或者如果有人有更好的建议。我试图完成的主要任务是必须是7,长度不能超过所有数字

我读过关于unique ID的文章,但上面说它不能保证,而且它有字母。有更好的方法吗

我遇到了这个问题,但是aspx是否可以按顺序生成我的函数?

尝试以下方法:

function generate_guid($length){
    $alphabet = '1234567890';
    $tip = array();
    do{
        $uniqueId = substr(str_suffel($alphabet), 0, $length );
        //check uniquekey in database like you check unique username
        //something like that
        $sql = 'select * from tablename where uniqueId = "'.$uniqueId.'"';//if your using PDO, bind accordingly
        $result = mysqli_query($conn, $sql);
    }while(mysqli_num_rows($result)); //if no rows, loop will break and you would get uniqu id in $uniqueId variable
return $uniqueId;
}
试试这个:

function generate_guid($length){
    $alphabet = '1234567890';
    $tip = array();
    do{
        $uniqueId = substr(str_suffel($alphabet), 0, $length );
        //check uniquekey in database like you check unique username
        //something like that
        $sql = 'select * from tablename where uniqueId = "'.$uniqueId.'"';//if your using PDO, bind accordingly
        $result = mysqli_query($conn, $sql);
    }while(mysqli_num_rows($result)); //if no rows, loop will break and you would get uniqu id in $uniqueId variable
return $uniqueId;
}

您可以尝试使用PHP从int生成unique

当散列数字时,将$len更改为7
您可以散列microtime或其他增量数字,以确保其唯一。

您可以尝试使用PHP从int生成unique

当散列数字时,将$len更改为7
您可以散列微时间或其他增量数字,以确保其唯一。

rand
不是非常随机的。来自[PHP rand]:()注意:此函数不会生成加密安全值,不应用于加密目的。如果需要加密的安全值,可以考虑使用RealthyIn()、RealthyByTeSe()、或OpenSSLIVRAPIONTIOOYBYTESE()来代替。在函数中,由于前零(s),字符串有10%的概率变成6位数。如果长度真的很重要。
rand
不是很随机的。来自[PHP rand]:()注意:此函数不会生成加密安全值,不应用于加密目的。如果需要加密的安全值,可以考虑使用RealthyIn()、RealthyByTeSe()、或OpenSSLIVRAPIONTIOOYBYTESE()来代替。在函数中,由于前零(s),字符串有10%的概率变成6位数。如果长度真的很重要。