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.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 4.67 out of 5)
Loading ... Loading ...
If you found this post useful, please consider a small donation.

13 Responses to “Php password generator”

  1. BFD on August 10th, 2006 4:57 am

    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.

  2. BFD on August 10th, 2006 5:07 am

    Sorry I forgot to include the file’s code:

    Note by Emanuele: the code you attached is incomplete. Waiting for your complete script

  3. BFD on August 11th, 2006 6:45 pm

    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

  4. Emanuele Feronato on August 11th, 2006 8:31 pm

    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!

  5. BFD on August 12th, 2006 2:19 am

    The function did not show, if you could please email me it.

  6. BFD on August 12th, 2006 2:20 am

    Nevermind, I saw the edits to your code.

  7. Bobby on December 25th, 2006 1:14 am

    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

  8. Sven on January 12th, 2007 11:11 am

    Nice, think :)

    i implemented that, works great :) nice think…

  9. Darrell on May 27th, 2007 2:13 am

    Thank you for this! I needed something exactly like this. Wonderful.

    Darrell Goodman
    Toronto, Canada

  10. J. Saxberg on September 2nd, 2007 9:58 pm

    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

  11. somebody on September 23rd, 2008 7:06 pm

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

  12. Unreal Media on November 24th, 2008 5:55 pm

    Great tool. I love the flexibility.

  13. big.nerd on April 8th, 2009 3:20 pm

    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

Leave a Reply




flash games company