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

You must javascript enabled to use this form

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 )

Add comments: PHP Password Generator

Enter your comment below:

(required)

(required)

Your email will not be displayed on the site; only to our administrator.

(optional)

For more BBCode info:  [Click here]

Supported BBCode

[b]bolded text[/b]
[i]italicized text[/i]
[u]underlined text[/u]
[s]striked text[/s]
[sub]subscripts text[/sub]
[sup]superscripts text[/sup]
[center]center text[/center]
[hr] To draw a line
[url]http://ongetc.com[/url]
To quote:[quote]quoted text[/quote]
[code]monospaced text[/code]
To change text size: [size=9]Your Text[/size]
To change text color: [color=red]Red Text[/color]
or [color=#FF0000]Red Text[/color]
(Can use many different color names or hex codes.)




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

Statistics

Content View Hits : 267783

Latest Comments


Software Box

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

WinMX Turbo Booster
It is a powerful plug-in for WinMX P2P application to boost up downloads speed and increase the amount of download sources.

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

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

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.

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

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 ...

Privacy Guard
Erase your Internet tracks, computer activities, and history. Your secrets are safe with Privacy Guard.

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

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

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

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

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

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

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

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.

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.

Rockrose Jingle
This is a sms / email / desktop reminder program. It provides flexible reminders for birthday, anniversary, medication, appointment or anything else.

Okoker CD&DVD Burner
This software is easy in use, burns CDs, DVDs and disc images without any problems, works fast and uses low resources.

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