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:
  1. <?php
  2. function create_password($length=8,$use_upper=1,$use_lower=1,$use_number=1,$use_custom=""){
  3.     $upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  4.     $lower = "abcdefghijklmnopqrstuvwxyz";
  5.     $number = "0123456789";
  6.     if($use_upper){
  7.         $seed_length += 26;
  8.         $seed .= $upper;
  9.     }
  10.     if($use_lower){
  11.         $seed_length += 26;
  12.         $seed .= $lower;
  13.     }
  14.     if($use_number){
  15.         $seed_length += 10;
  16.         $seed .= $number;
  17.     }
  18.     if($use_custom){
  19.         $seed_length +=strlen($use_custom);
  20.         $seed .= $use_custom;
  21.     }
  22.     for($x=1;$x<=$length;$x++){
  23.         $password .= $seed{rand(0,$seed_length-1)};
  24.     }
  25.     return($password);
  26. }
  27. ?>

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:

PHP:
  1. <?php
  2. echo create_password(); // Returns for example a7YmTwG4
  3. echo create_password(16); // Returns for example Z77OzzS3DgV3OxxP
  4. echo create_password(8,0,0); // Returns for example 40714215
  5. echo create_password(10,1,1,1,";,:.-_()"); // Returns for example or)ZA10kpX
  6. ?>

Enjoy and give me feedback.

Improve the blog rating this post
Tell me what do you think about this post. I'll write better and better entries.
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

10 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

Leave a Reply