View Single Post
Old 08-08-2012, 09:40 PM   PM User | #2
Arcticwarrio
Regular Coder

 
Arcticwarrio's Avatar
 
Join Date: May 2012
Location: UK
Posts: 574
Thanks: 15
Thanked 64 Times in 64 Posts
Arcticwarrio is on a distinguished road
i made this for adding salt to a password also for temporary passwords or pin numbers, could also be used for creating password reset keys

unique string chars or duplicated,
upper case, lowercase + numbers
Upper case + numbers
Numbers only

feel free to message me if you need help modifying it for your own needs
could easily be altered to generate lottery numbers etc

function:
PHP Code:
function GenCode($len,$type 1,$uni 0){
    
//Written by Arcticwarrio - 08-August-2012
    //http://www.codingforums.com/member.php?u=147238
    //GenCode(length of string,$type = 1, 2 or 3,unique chars?)
    //Unique Switch 1 for on 0 or ommited for off
    //Type Switch 1 = a-z, A-Z, 0-9     Usage: GenCode(8); or GenCode(8,1);
    //Type Switch 2 = A-Z, 0-9          Usage: GenCode(10,2);
    //Type Switch 2 = 0-9               Usage: GenCode(4,3);

    //Reset Function
    
$code ='';
    
//Array of chars to choose from, feel free to add more arrays
    
$codestring['1'] = array('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');
    
$codestring['2'] = array('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');
    
$codestring['3'] = array('1','2','3','4','5','6','7','8','9','0');
    
    
//see if the string should contain unique chars
    
if ($uni == 1){
        
//check if theres enough chars to have a unique string
        
if ($len count($codestring[$type])-1){ echo "error length too long for unique string"; exit;}
        
//loop for lenght of $len NOT allowing duplicate chars
        
for ($i=1$i<=$len$i++){
            
//add char from chosen array to the end of the variable $code
            
$tempx $codestring[$type][rand(0,count($codestring[$type])-1)];
            if (
strstr($code$tempx) != false){
                
$i--;
            }else{
                
$code .= $tempx;
            }
        }
 
    }else{
        
//loop for lenght of $len allowing duplicate chars
        
for ($i=1$i<=$len$i++){
            
//add char from chosen array to the end of the variable $code
            
$code .= $codestring[$type][rand(0,count($codestring[$type])-1)];
        }
    }
        
//send $code back to where GenCode() was called from
    
return $code;

__________________
There are 10 types of people on CodingForums,
Those who understand Binary and those who dont.
Arcticwarrio is offline   Reply With Quote