• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Generate code without guess

#3
Here is something I use.

PHP Code:
   public function generate_code() {
 
       // generate a randomish code
 
       $code bin2hex(pack('N'microtime(TRUE))).bin2hex(get_instance()->security->get_random_bytes(4));
 
       return $code;
 
   

It was suggested to me here on these forums somewhere by Narf ages ago but I use it all the time on almost every site I do. It is great for generating unique codes for urls, for all sorts of records, rather than using id. I have it in a functions model that has all my useful functions in. You could use more random bytes if you wanted a longer code.

Generates something like: 59aad953a91644ce

Hope it helps,

Paul

PS This is not for security, this is just to generate a code I can use instead of an ID, i.e.
Code:
Not: www.mysite.co.uk/view/record/4
But: www.mysite.co.uk/view/record/59aad953a91644ce
You still have to check it is unique when storing, and when using to retrieve a record that the user has permission to access it.
Reply


Messages In This Thread
Generate code without guess - by omid_student - 09-29-2017, 05:27 AM
RE: Generate code without guess - by rtenny - 09-29-2017, 07:05 AM
RE: Generate code without guess - by omid_student - 09-29-2017, 01:06 PM
RE: Generate code without guess - by PaulD - 09-29-2017, 10:43 AM
RE: Generate code without guess - by Paradinight - 09-29-2017, 11:55 AM
RE: Generate code without guess - by omid_student - 09-29-2017, 01:09 PM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.