Php password generator
Filed Under Php •
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.
Tell me what do you think about this post. I'll write better and better entries.
11 Responses to “Php password generator”
Leave a Reply

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;
}