每次生成一个10位数的唯一ID [英] Generate a 10 digit unique id every time

查看:172
本文介绍了每次生成一个10位数的唯一ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写脚本,基本上用户必须输入他的名字,选择文件,文件注释,如果需要并上传。

文件注释,名称,文件名将存储在数据库中使用auto_increment id。该文件也会上传到服务器。问题是我想生成一个10位数的唯一代码,这使用户可以轻松下载文件,他只需要输入10位数的代码,我的脚本就会将文件发送给他。


一切都很好,但问题是我无法生成10位唯一代码。安全性在这里并没有太多关注,只有id必须在每次失败时都是唯一的。由于脚本依赖于这个唯一的id来传递文件(在db中搜索这个唯一的id,从那里获取文件名并传递文件)。


我尝试了很多方法,但是不能保证id每次都是唯一的。


[PHP]< ;?

//设置随机id长度

$ random_id_length = 10;

//生成随机ID加密并将其存储在$ rnd_id中

$ rnd_id = crypt(uniqid(rand(),1));

//删除可能出现的任何斜线

$ rnd_id = strip_tags(stripslashes($ rnd_id));

//删除任何。或/和翻转字符串

$ rnd_id = str_replace("。","",$ rnd_id);

$ rnd_id = strrev(str_replace(" /","",$ rnd_id));

//最后我从$ rnd_id获取前10个字符

$ rnd_id = substr($ rnd_id,0,$ random_id_length);

echo" Random Id:$ rnd_id" ;;

?> [/ PHP]


上面的代码生成一个id,但我无法保证它的独特性。

如果它返回相同的ID怎么办?


有没有办法做到这一点?或者将auto_increment id从数据库转换为唯一id(加密)的任何函数,并在解密时产生相同的auto_increment id?


有什么方法吗?请帮帮我。

谢谢。

I am coding a script , where basically the user has to enter his name , choose file , file comments if required and upload.
The file comments , name , filename will be stored in the database with auto_increment id. The file gets uploaded to the server too. The problem is I want to generate a 10 digit unique code , which makes it simple for the user to download the file , he just has to enter the 10-digit code , and my script will deliver the file to him.

Everything is fine , but the problem is I am not able to generate the 10 digit unique code. Security is not focused much here , only the id has to be unique everytime WITHOUT FAIL. As the script depends on this unique id for delivering the file (searches the db for this unique id , gets the filename from there and delivers the file).

I have tried many ways , but cant assure the id will be unique everytime.

[PHP]<?
//set the random id length
$random_id_length = 10;
//generate a random id encrypt it and store it in $rnd_id
$rnd_id = crypt(uniqid(rand(),1));
//to remove any slashes that might have come
$rnd_id = strip_tags(stripslashes($rnd_id));
//Removing any . or / and reversing the string
$rnd_id = str_replace(".","",$rnd_id);
$rnd_id = strrev(str_replace("/","",$rnd_id));
//finally I take the first 10 characters from the $rnd_id
$rnd_id = substr($rnd_id,0,$random_id_length);
echo "Random Id: $rnd_id";
?>[/PHP]

The code above generates an id , but I am not able to assure its unique always.
What if it returns the same id ?

Is there a way to do this ? Or any function to convert the auto_increment id from the database to a unique id (encrypted) and result the same auto_increment id when decrypted ?

Any ways ? Please help me.
Thanks.

推荐答案

random_id_length = 10;

//生成随机ID加密并将其存储在
random_id_length = 10;
//generate a random id encrypt it and store it in


rnd_id
rnd_id


rnd_id = crypt (uniqid(RAND(),1));

//删除可能出现的任何斜线
rnd_id = crypt(uniqid(rand(),1));
//to remove any slashes that might have come


这篇关于每次生成一个10位数的唯一ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆