Posted By

fackz on 05/29/09


Tagged

password generation


Versions (?)

Who likes this?

6 people have marked this snippet as a favorite

fackz
jfherring
rocktronica
vali29
roflman79
esritter


Php password generator


 / Published in: PHP
 

Let' see the parameters

lenght: is the password length (default = 8)

use_upper: set to 0 if you do not want to use uppercase chars (ABCD...), any other value otherwise. Default = 1

uselower: set to 0 if you do not want to use lowercase chars (abcd...), any other value otherwise. Default = 1 usenumber: set to 0 if you do not want to use number chars (0123...), any other value otherwise. Default = 1

usecustom: a string representing any extra char you want (such as ?* ...). Default = empy string

  1. <?php
  2. function create_password($length=8,$use_upper=1,$use_lower=1,$use_number=1,$use_custom=""){
  3. $upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  4. $lower = "abcdefghijklmnopqrstuvwxyz";
  5. $number = "0123456789";
  6. if($use_upper){
  7. $seed_length += 26;
  8. $seed .= $upper;
  9. }
  10. if($use_lower){
  11. $seed_length += 26;
  12. $seed .= $lower;
  13. }
  14. if($use_number){
  15. $seed_length += 10;
  16. $seed .= $number;
  17. }
  18. if($use_custom){
  19. $seed_length +=strlen($use_custom);
  20. $seed .= $use_custom;
  21. }
  22. for($x=1;$x<=$length;$x++){
  23. $password .= $seed{rand(0,$seed_length-1)};
  24. }
  25. return($password);
  26. }
  27. ?>
  28.  
  29. USAGE:
  30.  
  31. <?php
  32. echo create_password(); // Returns for example a7YmTwG4
  33. echo create_password(16); // Returns for example Z77OzzS3DgV3OxxP
  34. echo create_password(8,0,0); // Returns for example 40714215
  35. echo create_password(10,1,1,1,";,:.-_()"); // Returns for example or)ZA10kpX
  36. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: roflman79 on October 9, 2009

thanks for this, helped me out

Posted By: AzizLight on October 26, 2009

I love it! Good Job!

You need to login to post a comment.