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>
Show (4) - Add comments:
|
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 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 |


