Posted By

dimkalinux on 05/30/13


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

andrewcolby


random_key


 / Published in: PHP
 

  1. function random_key($len, $readable = false, $hash = false)
  2. {
  3. $key = '';
  4.  
  5. if ($hash)
  6. $key = substr(sha1(uniqid(rand(), true)), 0, $len);
  7. else if ($readable)
  8. {
  9. $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  10.  
  11. for ($i = 0; $i < $len; ++$i)
  12. $key .= substr($chars, (mt_rand() % strlen($chars)), 1);
  13. }
  14. else
  15. for ($i = 0; $i < $len; ++$i)
  16. $key .= chr(mt_rand(33, 126));
  17.  
  18. return $key;
  19. }

Report this snippet  

You need to login to post a comment.