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.
-
<?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 .= $use_custom;
-
}
-
for($x=1;$x<=$length;$x++){
-
}
-
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:
Enjoy and give me feedback.
13 Responses to “Php password generator”
Leave a Reply
Posts
- Rick Triqui: my first PlayCrafter game
- Prototype of a Flash game like Meeblings
- Games for the game developers!
- The art of debugging
- How to embed a text file in Flash
- Create a Flash game in minutes with PlayCrafter
- Upgrade your Flash CS4 to 10.0.2
- Play Mazeroll, my latest Box2D game
- Triqui MochiAds Arcade plugin for WordPress Released!!
- The MochiAds funnel
- Flash game creation tutorial - part 1
- Create a Lightbox effect only with CSS - no javascript needed
- Flash game creation tutorial - part 2
- Make a Flash game like Flash Element Tower Defense - Part 2
- Flash game creation tutorial - part 3
- Create a flash draw game like Line Rider or others - part 1
- Create a Flash Racing Game Tutorial
- Make a Flash game like Flash Element Tower Defense - Part 1
- Create a flash artillery game - step 1
- Create a flash draw game like Line Rider or others - part 5
- Flash game creation tutorial – part 5.2




(4.9 out of 5) - Flash game creation tutorial – part 3




(4.86 out of 5) - Creation of a platform game with Flash – step 2




(4.84 out of 5) - Create a survival horror game in Flash tutorial – part 1




(4.82 out of 5) - Create a flash artillery game – step 1




(4.82 out of 5) - Create a Flash Racing Game Tutorial




(4.8 out of 5) - Create a flash artillery game – step 2




(4.75 out of 5) - New tile based platform engine – part 6 – ladders




(4.74 out of 5) - Flash game creation tutorial – part 2




(4.73 out of 5) - The experiment – one year later




(4.7 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