Php password generator


/ Published in: PHP
Save to your folder(s)

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

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

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


Copy this code and paste it in your HTML
  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

You need to login to post a comment.