PHP Random Password Generator

27 July 2011 by Rick ~ 8 Comments 5012 views

PHP

How to create a random password generator with PHP

I found this cool snippet and I wanted to share it with you.

The code basically uses predefined characters to create a string.

You can set the length and the strength.

<?php
 
function generatePassword($length=9, $strength=0) {
    $vowels = 'aeuy';
    $consonants = 'bdghjmnpqrstvz';
    if ($strength & 1) {
        $consonants .= 'BDGHJLMNPQRSTVWXZ';
    }
    if ($strength & 2) {
        $vowels .= "AEUY";
    }
    if ($strength & 4) {
        $consonants .= '23456789';
    }
    if ($strength & 8) {
        $consonants .= '@#$%';
    }
 
    $password = '';
    $alt = time() % 2;
    for ($i = 0; $i < $length; $i++) {
        if ($alt == 1) {
            $password .= $consonants[(rand() % strlen($consonants))];
            $alt = 0;
        } else {
            $password .= $vowels[(rand() % strlen($vowels))];
            $alt = 1;
        }
    }
    return $password;
}
 
?>

Now you just need to call the function wherever you want.

Your new password is <?=generatePassword(9,8)?>

Subscribe by E-mail:

« »

8 Comments

  1. Hey

    I am looking at the categories on the right. Do you also write about a forum creation?

    thanks
    Nicole :)

     
  2. hey

    I need a forum that I can easily customize and manage. that would be great from the SEO perspective.

    I have installed phpBB, and updated it to the newest version, but it is confusing, as i have no idea where to begin, I would like to customize it, but see no manuals.

    SMf looks simpler. Thank you for the link.
    I would use it.

    best
    N.

     
    • Never did it on phpBB or SMF but shouldn’t be hard. Look for the template folder and happy coding :P

      Good luck ;)

       
  3. I have one more question.

    I see in the SEO Quake panel the date when you website was created. It is showed by “Webarchive age”. Did you do anything so they have your website archived?
    i have a few websites, but none of them has this feature structured.

     
    • Using the extension you saw CodeGround’s creation date?

      I didn’t do anything. It has to be something about WordPress.

      Does the date match the first post date?

       
  4. it says February 7, 2008 . Is it the date when you created the site?

     

Add a Comment

Use [code] example piece of code [/code] to insert code into your comment.