Php password generator
- August 2, 2006 by Emanuele Feronato
- Filed under Php | 13 Comments
Today I had to generate some passwords, and I am not so creative in doing this.
So I coded a function I want to share with you.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <?php function create_password($length=8,$use_upper=1,$use_lower=1,$use_number=1,$use_custom=""){ $upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; $lower = "abcdefghijklmnopqrstuvwxyz"; $number = "0123456789"; if($use_upper){ $seed_length += 26; $seed .= $upper; } if($use_lower){ $seed_length += 26; $seed .= $lower; } if($use_number){ $seed_length += 10; $seed .= $number; } if($use_custom){ $seed_length +=strlen($use_custom); $seed .= $use_custom; } for($x=1;$x<=$length;$x++){ $password .= $seed{rand(0,$seed_length-1)}; } return($password); } ?> |
How does it work?
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
Examples:
1 2 3 4 5 6 | <?php echo create_password(); // Returns for example a7YmTwG4 echo create_password(16); // Returns for example Z77OzzS3DgV3OxxP echo create_password(8,0,0); // Returns for example 40714215 echo create_password(10,1,1,1,";,:.-_()"); // Returns for example or)ZA10kpX ?> |
Enjoy and give me feedback.
13 Responses
Leave a Reply
TUTORIAL SERIES:
- Una guida completa al gioco del poker online e una selezione dei migliori casino online.
- casino online
- migliori casino online
- BlackJack online
- casinò online

(3 votes, average: 4.67 out of 5)

I need help with this, I added everything in and I’m kind of a n00b to php. I put the file here, http://beta.theblackhole.be/cc-common/functions.php Please take a look and tell me what I did wrong.
Sorry I forgot to include the file’s code:
Note by Emanuele: the code you attached is incomplete. Waiting for your complete script
Sorry it wouldn’t let me include the entire code, I have added the file to a zip archive for you to look at.
http://theblackhole.be/download.php?f=functions.zip
Well… you have to include your function between php tags.
I mean you open with < ?php then you paste the function, then the echo and finally you close php with ?>
Good luck!
The function did not show, if you could please email me it.
Nevermind, I saw the edits to your code.
Works great! Did some editing to get it to do what we needed and it works as advertised. See it in action at http://www.mywebteks.com/hosting_store/pass_gen/pass_generator.php
Nice, think :)
i implemented that, works great :) nice think…
Thank you for this! I needed something exactly like this. Wonderful.
Darrell Goodman
Toronto, Canada
function makePasswd ($length = 6, $useUpper = false, $useLower = true, $useNumber = true, $useCustom = ”) {
$upper = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ’;
$lower = ‘abcdefghijklmnopqrstuvwxyz’;
$number = ’0123456789′;
$seed = ”;
$password = ”;
$seedLength = 0;
if ($useUpper) {
$seedLength = 26;
$seed .= $upper;
}
if ($useLower) {
$seedLength = 26;
$seed .= $lower;
}
if ($useNumber) {
$seedLength = 10;
$seed .= $number;
}
if ($useCustom != ”) {
$seedLength = strlen($useCustom);
$seed .= $use_custom;
}
for ($x = 1; $x
an optimization for your code, same functionality, consume less memory and should be faster.
function generatePassword($length = 8,$use_upper = true,$use_lower = true, $use_number = true, $use_custom=”"){
$seed = ”;
$seed .= ($use_upper ? ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ’ : ”);
$seed .= ($use_lower ? ‘abcdefghijklmnopqrstuvwxyz’ : ”);
$seed .= ($use_number ? ’0123456789′ : ”);
$seed .= ($use_custom ? $use_custom : ”);
$seed_length = strlen($seed);
$password = ”;
for($x = 0; $x < $length; $x++){
$password .= $seed[rand(0, $seed_length-1)];
}
return $password;
}
Great tool. I love the flexibility.
Excellent Function, thank you!, Also, thanks to ‘somebody’ for the optimization.
One note though…
I would remove 1′s, I’s, O’s and 0′s, with some screen’s, and fonts, these can look very similar, especially when generating a random password to give to a user.
If the password is being used internally, or for a technically advanced individual, or even for some other function (such as a custom password salt) you can always increase the potential password combination’s by adding them back in via the $use_custom variable, in which case special char’s would be recommended for use anyways, and as well, it can be a bit of work, but non-english char’s.
Keep up the good work!! Thanks again!
big.nerd