Posted By

kirik on 08/06/09


Tagged

password random generator String


Versions (?)

Who likes this?

5 people have marked this snippet as a favorite

brent-man
thastyle
irishsk
umang_nine
fackz


random string generator (for passwords)


 / Published in: PHP
 

Function for generating random strings

  1. function rndstr($length=11, $type='a-zA-Z0-9')
  2. {
  3. $return = $chars = null;
  4.  
  5. if(strstr($type, 'a-z'))
  6. $chars .= 'abcdefghijklmnopqrstuvwxyz';
  7. if(strstr($type, 'A-Z'))
  8. $chars .= 'ABCDEFGHIJKLMNOPRQSTUVWXYZ';
  9. if(strstr($type, '0-9'))
  10. $chars .= '0123456789';
  11.  
  12. for($i = 0, $sl = strlen($chars) - 1; $i <= $length; $i++)
  13. $return .= $chars[rand(0, $sl)];
  14.  
  15. return $return;
  16. }
  17. /** **** Examples ****
  18. --- We need alphanumeric string 5 chars length ---
  19. echo rndstr(5);
  20. --- We need lowercase letters string 10 chars length ---
  21. echo rndstr(10, 'a-z');
  22. --- We need lower and upper case letters string 10 chars length ---
  23. echo rndstr(10, 'A-Za-z');
  24. --- We need numeric string 10 chars length ---
  25. echo rndstr(10, '0-9');
  26. --- ect.. ---
  27. **/

Report this snippet  

You need to login to post a comment.