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 : 267773

Latest Comments


Software Box

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.

NaviCOPA Web Server
NaviCOPA takes the hard work out of running a Web Server. NaviCOPA supports an unlimited number of virtual hosts, PHP, SSL and generates industry standard W3C log files so you can analyse your web ...

Nevo Audio Splitter
It is a powerful and ease-to-use audio splitter. It can split mp3 and wma files.

iPod Photo Slideshow
This program allows you to create entertaining MPEG-4 photo slideshows playable on iPod.

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

World 3D Cup 2006 Match Player
Hottest World Cup moments in 3D. Unique video replay on your PC.

Christmas Candlelight Living Desktop
Christmas comes alive on your desktop with these beautiful candlelit Christmas scenes.

dvdXsoft DVD to Zune Converter
This program can convert video DVD to MP4 Video files suitable for Zune. Watch your favorite movies mobile.

LimeWire Acceleration Patch
This program is a module created to boost up LimeWire P2P.

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

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.

Pokie Magic Egyptian Dreams 3
It is an Aussie style 20 payline 6 reel poker machine with an Egyptian theme. Win up to 51 free spins in this six reel pokie extravaganza.

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.

Pokie Magic Maidens Treasure
It is an Aussie style 20 payline 5 reel poker machine (also called a slot machine or a "Pokie").

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

Jesterware PSP Video Suite
It is a professional video converter for Sony PSP consoles. You can easily and quickly convert videos in all popular formats to Sony PSP compliant video files.

PowerPoint Viewer OCX
PowerPoint SlideShow ActiveX Control. The OCX is lightweight and flexible, and gives developers new possibilities for using Microsoft PowerPoint in a custom solution.

Spherical Panorama Fisheye Stitcher
Utility for create spherical panorama logotype. Gaussian and Sharpen correction of seam.

Innovatools Add / Remove Plus! 2006
Remove programs you no longer want on your computer.