Php password generator
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 to “Php password generator”
Leave a Reply
- My epic fail with ClickBank
- Get up to $100,000 for your next Flash game with Mochi GAME Developer Fund
- Create a dynamic content animated footer ad for your site in just 9 jQuery lines – 17 lines version
- Sell sitelocked version of your Flash games and even .fla sources to Free Online Games
- Protect your work from ActionScript code theft with SWF Protector
- Create a dynamic content animated footer ad for your site in just 9 jQuery lines
- Understanding Box2D’s one-way platforms, aka CLOUDS
- Triqui MochiAds Arcade plugin for WordPress upgraded to 1.2
- Box2D Flash game creation tutorial – part 2
- 11 Flash isometric engines you can use in your games
- Create a Lightbox effect only with CSS - no javascript needed
- Flash game creation tutorial - part 1
- Create a Flash Racing Game Tutorial
- Flash game creation tutorial - part 2
- Make a Flash game like Flash Element Tower Defense - Part 2
- Flash game creation tutorial - part 3
- Make a Flash game like Flash Element Tower Defense - Part 1
- Create a flash draw game like Line Rider or others - part 1
- Triqui MochiAds Arcade plugin for WordPress official page
- Create a flash artillery game - step 1
- Flash game creation tutorial – part 5.2 (4.88/5)
- Create a flash artillery game – step 1 (4.79/5)
- Create a Flash Racing Game Tutorial (4.76/5)
- Create a flash artillery game – step 2 (4.74/5)
- Create a survival horror game in Flash tutorial – part 1 (4.73/5)
- Creation of a Flash arcade site using WordPress – step 2 (4.73/5)
- Flash game creation tutorial – part 2 (4.71/5)
- Flash game creation tutorial – part 1 (4.70/5)
- Create a flash draw game like Line Rider or others – part 1 (4.69/5)
- Creation of a platform game with Flash – step 2 (4.68/5)

(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