Posts: 228
Threads: 7
Joined: Sep 2010
Reputation:
5
12-05-2011, 02:38 PM
(This post was last modified: 12-05-2011, 02:46 PM by ★Cooldude★.)
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:
Output: aAWfUxZyGTeb56%
Usage:
(09-05-2011, 08:36 AM)Orgy Wrote: If you understand what you're doing, you aren't learning anything. ;)
Posts: 2,216
Threads: 187
Joined: Jul 2011
Great release CoolDude, I think you're going to fit in nicely here at SF Coders.
This was great for your first project.
Posts: 228
Threads: 7
Joined: Sep 2010
Reputation:
5
(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. Do you have any suggestions for it?
(09-05-2011, 08:36 AM)Orgy Wrote: If you understand what you're doing, you aren't learning anything. ;)
Posts: 103
Threads: 5
Joined: Nov 2011
Reputation:
2
(12-05-2011, 03:23 PM)★Cooldude★ Wrote: Thanks. Do you have any suggestions for it?
It's pretty nice and congratulations on your entry into the coders!
Posts: 228
Threads: 7
Joined: Sep 2010
Reputation:
5
(12-05-2011, 08:09 PM)Infinity Wrote: It's pretty nice and congratulations on your entry into the coders!
Thank you.
(09-05-2011, 08:36 AM)Orgy Wrote: If you understand what you're doing, you aren't learning anything. ;)
Posts: 122
Threads: 14
Joined: Sep 2010
Reputation:
3
Haha, It will also be your last - I kid, It will not be you last.
On-Topic: Great work man, I Like to see projects outside of VB, This was interesting.
"If you cannot explain something simply, then you do not understand it at all." - Albert Einstein
"The best way to predict the future is to invent it." - Alan Kay
Posts: 228
Threads: 7
Joined: Sep 2010
Reputation:
5
Posts: 10
Threads: 2
Joined: Aug 2011
Reputation:
0
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.
Posts: 228
Threads: 7
Joined: Sep 2010
Reputation:
5
^That's very interesting, you've introduced me to some new functions.
(09-05-2011, 08:36 AM)Orgy Wrote: If you understand what you're doing, you aren't learning anything. ;)
|