Nurv-OS

...because your system matters to you

  • Increase font size
  • Default font size
  • Decrease font size

PHP Password Generator

User Rating: / 510
PoorBest 

PHP Password Generator

Provided by Nurv-OS.com

Nurv-OS and Jason Christman bring you a simple and easy to use password generator.  The generatePassword function does not have any requirements you can pass a strength level of 1-5 or do not pass it anything and it will use the default level which is strong (explained in code).  Simple copy and paste the function and start calling it.

Example Call: $password = generatePassword(); //$password would now hold a password like "E?nM9o9"

This will return a password that is 7 characters long with 1 special character and 2 upper case.

All generated passwords will start with a Character to accommodate different systems. Please enjoy the code and let us know of any modifications or suggestions we can do with it.  The code is free based on the open-source licensing.  

Demo Password Generator and watch it build a password.



Code Sample (PHP)

<?php


$s = 5;
$password = generatePassword($s);


function generatePassword($strength = null ) {
/**********************************************************************************************************************************************************
* Author: Jason Christman
* Program: generatePassword()
* Date: 07/06/09
* Description: PHP Password Generator
*         : This script utilizes a password strength building process
*       : Password Strength levels   :
*           :Minimum  : 1 : lenth of 4 characters being numbers and lower case
*           :Base     : 2 : length of 5 characters with a reqirement of 2 Upper case and the reast being numbers and lower case
*           :Strong   : 3 : length of 7 characters with a requirement of 1 Special Characters + minimum
*           :Stronger : 4 : length of 12 characters with a requirement of 2 Special Charcters + minimum
*           :Maximum  : 5 : length of 23 with a requirement of 3 Special Characters and 3 Upper Case + minimum
*
*
**************************************************************************************************************************************************************
*                           *** (Please give credit and send any updates back to This e-mail address is being protected from spambots. You need JavaScript enabled to view it ) Thank you ***
**************************************************************************************************************************************************************/
    
    
    /*************************************************************
    * Password Strenth Levels
    *************************************************************/
    
    $base = 2;
    $strong = 3;
    $stronger = 4;
    $maximum = 5;
    
    /*******************************************************
    * Set default strength level
    *******************************************************/
    
    if($strength == null){ $strength = $strong; }
    if(!is_numeric($strength)){ $strength = $strong; }
    if($strength > 5){ $strength = $maximum; }
    
    /******************************************
    * Initialize variables
    ******************************************/
    
    $length = 4;
    $slength = 0;
    $choice = 0;
    $sMin = 0;
    $uMin = 0;
    $uCnt = 0;
    $sCnt = 0;
    $symbol = false;
    $upper = false;
    $numbers = '0123456789';
    $lower_case = 'asdfghjklzxcvbnmqwertyuiop';
    $upper_case = 'ASDFGHJKLZXCVBNMQWERTYUIOP';
    $symbol_chr = '/[|!@#$%&*\/(=?;).:\-_+~^\\]';
    $password = '';
    
    /************************************************************
    * Set variables for building password based on strength
    ************************************************************/

    if($strength >= $base){ $upper = true; $uMin = 2; $length = 5;}
    if($strength >= $strong){ $symbol = true; $sMin = 1; $length = 7;}
    if($strength >= $stronger){ $sMin = 2; $length = 12; }
    if($strength >= $maximum){ $sMin = 3; $uMin = 3; $length = 23;}
    
    /***********************************************************
    * Build Password Based on strenth level requirements
    ***********************************************************/
    while($slength < $length){
        $choice = rand(1,4);
           
        if($choice == 1){ // Check to add upper case characters
            if($upper == true){
                preg_match_all('/[A-Z]/', $password, $uppercase_characters);
                 if (!empty($uppercase_characters)){
                    $uCnt= count($uppercase_characters[0]);
                    if($uCnt < $uMin){
                        $password .= $upper_case[(rand() % strlen($upper_case))];    
                    }
                }
                   
            }
        }
        if($choice == 2){ // add lower case characters
            if(($length - $slength) > (($uMin - $uCnt) + ($sMin - $sCnt))){    
                $password .= $lower_case[(rand() % strlen($lower_case))];
            }
        }
        if($choice == 3){ // add numbers
            if(($slength > 0) && (($length - $slength) > (($uMin - $uCnt) + ($sMin - $sCnt)))){        
                $password .= $numbers[(rand() % strlen($numbers))];
            }
        }
        if($choice == 4){ // check to add special characters
            if($symbol == true){
                preg_match_all('/[|!@#$%&*\/(=?;).:\-_+~^\\\]/', $password, $symbols);
                if (!empty($symbols)){
                    $sCnt = count($symbols[0]);
                    if (($sCnt < $sMin) && $slength > 0){
                        $password .= $symbol_chr[(rand() % strlen($symbol_chr))];
                    }
                }        
            }
        }
        $slength = strlen($password);

    }
    
    
    return $password;
}
?>

 

 


HTML CODE

 

 

 

<form action="./Password_Strength.php" method="post">
<div align="center">
<input type="checkbox" name="sgen" />Show generated password progression
</div>
<div align="center">
<select name="level">
<option value="">Select Strength Level</option>
<option value=""></option>
<option value="1"<?php if(isset($_POST['level']) && $_POST['level'] == 1){ echo "selected='selected'";}?> >Minimum</option>
<option value="2"<?php if(isset($_POST['level']) && $_POST['level'] == 2){ echo "selected='selected'";}?>  >Base</option>
<option value="3"<?php if(isset($_POST['level']) && $_POST['level'] == 3){ echo "selected='selected'";}?>  >Strong</option>
<option value="4"<?php if(isset($_POST['level']) && $_POST['level'] == 4){ echo "selected='selected'";}?>  >Stronger</option>
<option value="5"<?php if(isset($_POST['level']) && $_POST['level'] == 5){ echo "selected='selected'";}?>  >Maximum</option>
</select>
  
<input type="submit" name="ssubmit" value="Generate Password" /><br />
<?php if(isset($_POST['ssubmit'])){ $spass = generatePassword($_POST['level']); echo "<br><b>" . $spass;}?>
</b>
</div>
</form>


 

Comments

I have added the code for the form portion of the page to show a sample use.

Posted by Jason, on Monday, 01 February 2010 at 15:31

Could you please post th form part of the code, I am very new to php and could really use a password generator like this
Thanks Sonny

Posted by Sonny, on Monday, 01 February 2010 at 11:13

Thanks for posting this function, this was very easy to include into our system and setup. We also like the ability to change security levels and other settings. The php password generator code was very easy to read and make since of what was taking place.

We have implemented this into our site for password reset requests which has taken the load off the helpdesk. Users now make the request and the system calls the function and emails them their new temporary password.

Posted by devn, on Tuesday, 24 November 2009 at 21:02

This is a good password generator function I have used this for my site for the automation of passwords when users forget them and request a new one. Thank you for posting the function, it is very easy to implement and use.

Posted by dev, on Tuesday, 24 November 2009 at 20:53

 1 
Page 1 of 1 ( 4 Comments )

Last Updated on Monday, 01 February 2010 21:30  
Donate using PayPal
Amount:
Note:

Statistics

Content View Hits : 267772

Latest Comments


Software Box

Arabic School Software
Learn Arabic using interactive multimedia Arabic lessons suitable for beginners, and ideal for children.

TextPipe Standard
It is an industrial strength text transformation workbench for data conversion and mining, electronic publishing and website maintenance.

Cute Reminder
Create desktop sticky notes, set up non-annoying reminders, and manage your ideas with just one or two mouse clicks.

Ultra Zune Video Converter
It is a professional Zune video / audio conversion software, that can convert movie to MPEG-4, H.264, WMV and MP3, WMA, M4A format with excellent quality.

Oxygen Phone Manager II for Nokia phones
A complete software tool to manage data and settings of your Nokia phone.

ArchiCrypt Shredder
It is data shredder and trace destroyer. It deletes sensitive data so completely that it is impossible to reconstruct it using special software.

Mumbo Jumbo
An addictive scrambled word game for all the Text Twist and Scrabble players.

Rename From List
Automatically rename multiple files from an imported plain text list of files or create new folders from a plain text folder list.

3D Pacman
Hilarious and highly addictive 3D remake of the legendary PacMan.

Picture Viewer and Converter Suite
Easy to use imaging software for viewing and converting your digital photos.

Flash Saver
Download flash fast (from webpage, from cache). Resume broken downloads. Powerful user-define functions. Includes flash player.

The Washington Memorial - Animated Wallpaper
This is an Animated Wallpaper of "The Washington Memorial" with music.

Software MIDI Keyboard Lite
The software emulates 25-keyed MIDI keyboard with 3 functional pedals.

Enchanted Forest - Animated Screensaver
In the amazing, fabulous forest among the lofty trees live fabulous creatures - bright butterflies, little luminous fairies and snow-white unicorn.

Binary Boy
A yEnc and NZB compliant newsreader with cache and scheduler. Download music, pictures, and movies from newsgroups quickly and conveniently.

Spherical Panorama SP_SC Converter
It is intended for compiling the panoramic scenes (spherical and cylindrical panoramas) in ready HTML web page or in PE/EXE module.

Magic Lines
It is a cute little of the beloved Lines. You have the square game board where you should move multicolored balls to make lines of five balls that match in color.

MING Chat Monitor
Monitor and record AOL, MSN, Yahoo, and ICQ conversations on your LAN in stealth mode.

PC iMail 2006
Create, Send and Track personalized HTML E-mail Newsletters in Minutes.

Audio Mid Recorder
This is a powerful real-time sound recorder. It is designed to work directly with your sound card, so it can record almost all audio from your sound card at near-perfect quality what you can hear f...