Random password generator component for CakePHP

In: Tutorials

11 Jun 2008

This is a component I put together for CakePHP 1.2 RC1 that will generate a random password. All you have to pass it is the length of the password.

To use it, make this file app/controllers/components/password_helper.php

You can use it in a controller by adding ‘PasswordHelper’ to the $components array.

You can adjust the $possible set with your desired characters.

Here is the code:

<?php
class PasswordHelperComponent extends Object {
 
/**
 * Password generator function
 *
 * This function will randomly generate a password from a given set of characters
 *
 * @param int = 8, length of the password you want to generate
 * @return string, the password
 */    
    function generatePassword ($length = 8)
    {
        // initialize variables
        $password = "";
        $i = 0;
        $possible = "0123456789bcdfghjkmnpqrstvwxyz"; 
 
        // add random characters to $password until $length is reached
        while ($i < $length) {
            // pick a random character from the possible ones
            $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
 
            // we don't want this character if it's already in the password
            if (!strstr($password, $char)) { 
                $password .= $char;
                $i++;
            }
        }
        return $password;
    }
}
?>

1 Response to Random password generator component for CakePHP

Avatar

Frank

August 31st, 2008 at 4:21 am

Thanks for this man, nice little helper.

Comment Form

SoliTech GmbH

Services such as Facebook, MySpace, LinkedIn, Twitter etc. are experiencing exponential growth. Your company needs to stay competitive and utilize this new technology. We will help you devise and execute a higly effective marketing strategy that will grow your brands' following and deliver measurable results.

Click here to see all our service.

What’s new in the forums?

  • New idea for the Toplist :D July 31, 2010
    OK, i was thinking about something... A top list inside of a clan. Im not sure where it would go though, maybe another tab with the clan name, view... […]
  • Add clan invites July 21, 2010
    I think we should bring back the option of add ninja to clan that way we can send invites to people. […]
  • Mass leveling system needs fixing July 20, 2010
    I know the mass leveling system has gotten to be a huge part of a ninja's life now, with the exp coming to us so easily, and the leveling coming just... […]
  • Transferring, fixed. July 20, 2010
    The transfer system greatly unbalanced the game, especially near the end (yet again im sorry for that), but would be it be possible to have it come... […]
  • Useless Clans July 16, 2010
    Clone Troopers Size: 0 Clone Troopers Size: 0 The Sereitei Size: 0 Empire […]
  • An item?...From a Ninja? July 16, 2010
    The title may confuse you on what i really mean, but i was thinking, is there a possibility that you could obtain items from killing other ninjas? it... […]
  • DP July 16, 2010
    I have been playing DuelingNinjas for a long time....and i still dont understand how the DP of armors are taken away. I can have 2bil health, yet... […]
  • Commas??? July 15, 2010
    I think it would be a lot easyer for ninjas these days if the levels, xp, yen, and hp had commas to divide the hundreds thousands and millions.... […]