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_leng th = 10;
//generate a random id encrypt it and store it in $rnd_id
$rnd_id = crypt(uniqid(ra nd(),1));
//to remove any slashes that might have come
$rnd_id = strip_tags(stri pslashes($rnd_i d));
//Removing any . or / and reversing the string
$rnd_id = str_replace("." ,"",$rnd_id) ;
$rnd_id = strrev(str_repl ace("/","",$rnd_i d));
//finally I take the first 10 characters from the $rnd_id
$rnd_id = substr($rnd_id, 0,$random_id_le ngth);
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.
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_leng th = 10;
//generate a random id encrypt it and store it in $rnd_id
$rnd_id = crypt(uniqid(ra nd(),1));
//to remove any slashes that might have come
$rnd_id = strip_tags(stri pslashes($rnd_i d));
//Removing any . or / and reversing the string
$rnd_id = str_replace("." ,"",$rnd_id) ;
$rnd_id = strrev(str_repl ace("/","",$rnd_i d));
//finally I take the first 10 characters from the $rnd_id
$rnd_id = substr($rnd_id, 0,$random_id_le ngth);
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.
Comment