Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-08-2012, 09:20 PM   PM User | #1
Arcticwarrio
Regular Coder

 
Arcticwarrio's Avatar
 
Join Date: May 2012
Location: UK
Posts: 597
Thanks: 15
Thanked 67 Times in 67 Posts
Arcticwarrio is on a distinguished road
Password, Salt, any length Random string Generator

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 i was that impressed with it i decided to share it with you all.

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
added lottery numbers generator

function:
PHP Code:
function GenCode($len,$type 1,$uni 0){
    
    
//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 3 = 0-9               Usage: GenCode(4,3,1);
    //Type Switch 4 = 0-49               Usage: GenCode(6,4,1); <-- Lottery Numbers

    //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');
    
$codestring['4'] = array('|1|','|2|','|3|','|4|','|5|','|6|','|7|','|8|','|9|','|10|','|11|','|12|','|13|','|14|','|15|','|16|','|17|','|18|','|19|','|20|','|21|','|22|','|23|','|24|','|25|','|26|','|27|','|28|','|29|','|30|','|31|','|32|','|33|','|34|','|35|','|36|','|37|','|38|','|39|','|40|','|41|','|42|','|43|','|44|','|45|','|46|','|47|','|48|','|49|');
    
    
//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)];
        }
    }
    
//clean up the code
    
$code str_replace('"','',$code);
    
$code str_replace('|',' ',$code);
        
//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.

Last edited by Arcticwarrio; 08-08-2012 at 11:23 PM.. Reason: Tweaked code to remove bug for two 00 in the unique pin
Arcticwarrio is offline   Reply With Quote
Reply

Bookmarks

Tags
generator, password, pin, random, string

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:50 PM.


Advertisement
Log in to turn off these ads.