Support Forums

Full Version: [SF Coders] Password Generator
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
[Image: YzwzC.png]

PHP Code:
function GeneratePass($length 5){
$length = (INT)$length//Type cast our variable to be an integer. If anything other than an integer was submitted, it will now equal 0 (With the exception of
                        //the boolean value 'true', which will cause the variable to now equal one.
                        
if ($length <5){ //The minimum length for the password is five, anything less isn't worth the time it takes to process.
$length 5;
}

$alphabet explode(",""a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,1,2,3,4,5,6,7,8,9,0");
$numbers explode(",""1,2,3,4,5,6,7,8,9,0");
$symbols explode(",""!,£,$,%,^,&,*,(,),{,},~,@,:,;,#,?,/,>,.,<");
//Split all the available letters, numbers and symbols up into an array.

$pass ""//Declare our password
while(strlen($pass) < $length){ 
if (
strlen($pass) == ($length -1)){
$choose rand(0, (sizeof($symbols) -1));
$pass .= $symbols[$choose];

}elseif (
strlen($pass) >=($length 3)){
$choose rand(0, (sizeof($numbers) -1));
$pass.= $numbers[$choose];
}else{
$choose rand(0, (sizeof($alphabet) -1));
$pass .= $alphabet[$choose];
}
}
Return 
mysql_real_escape_string(strip_tags($pass));


Above is a function which will generate a password of a given length. A minimum length of 5 is required. The generated password is then sanitised ready for use in a database.
The given password will be in the following format:

[(Given length -3)X A-Z] [2X 0-9] [1 X Symbol]

Example:

PHP Code:
GeneratePassword(15

Output: aAWfUxZyGTeb56%

Usage:
PHP Code:
GeneratePassword(LENGTH
Great release CoolDude, I think you're going to fit in nicely here at SF Coders.

This was great for your first project.
(12-05-2011, 03:09 PM)BreShiE Wrote: [ -> ]Great release CoolDude, I think you're going to fit in nicely here at SF Coders.

This was great for your first project.

Thanks. Smile Do you have any suggestions for it?
(12-05-2011, 03:23 PM)★Cooldude★ Wrote: [ -> ]Thanks. Smile Do you have any suggestions for it?

It's pretty nice and congratulations on your entry into the coders!
(12-05-2011, 08:09 PM)Infinity Wrote: [ -> ]It's pretty nice and congratulations on your entry into the coders!

Thank you. Smile
Haha, It will also be your last Tongue - I kid, It will not be you last.

On-Topic: Great work man, I Like to see projects outside of VB, This was interesting.
(12-06-2011, 07:26 AM)milopeach Wrote: [ -> ]Haha, It will also be your last Tongue - I kid, It will not be you last.

On-Topic: Great work man, I Like to see projects outside of VB, This was interesting.

:O Nooo. Sad

Thanks. I am actually planning on creating a VB.Net version. Tongue
PHP Code:
function passgen($len 5){
$alphau = Array ('A','B','C''D''F''G' ,'H''J''K''L''M''N','O''P''Q''R''S','T''U''V''W''X''Y','Z' );
$alphal = array('a','b','c','d','f','g','h','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
$misc = array('1','2','3','4','5','6','7','8','9','[',']','{','}','_','^','%','#','@','+','-');
$list array_merge($alphau,$alphal,$misc);
shuffle($list);
$password '';
for(
$i=0;$i<$len;$i++) {
$add array_rand($list);
$password .=  $list[$add];
}
return 
$password;

Thats my version.
^That's very interesting, you've introduced me to some new functions. Big Grin