Random Password Generator.-VBForums
Results 1 to 2 of 2

Thread: Random Password Generator.

  1. #1

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    564

    Random Password Generator.

    Hi I started to learn a little php today and managed to convert one of my C# codes to php this example allows you to make random password.
    Hope you like it,comments and suggestions welcome.

    PHP Code:
    <?php

    /**
     * @author Ben-Jones
     * @version 1.0
     * @name Rnadom Password Generator.
     * Script to generate random password by a given length.
     */
     
    function GenPassword($Length){
        
        
    $patten "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
        
        
    //Exit of no length found.
        
    if ($Length==0) return;
        
        if (!
    is_integer($Length)){
            echo 
    "Syntext error not a vaild integer.";
            return;
        } else {
          
          for(
    $x=0;$x<$Length;$x++){
            
    //Get random number.
            
    $rnd rand() % strlen($patten);
            
    //Build Random Password.
            
    $buff .= $patten[$rnd];
          } 
        }
        
    //Return the generated password.
        
    return $buff;
    }

    //Example.
    //Get password length.
    $plen = (int) $_GET["len"];
    //Return password.
    $password "Your New Password is: <b>".GenPassword($plen)."</ b>";
    //Output random password.
    echo $password;

    ?>

  2. #2

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    564

    Post Re: Random Password Generator V1.1

    hi just thought I show you an update. Today I managed to convert my random password generator to a class.
    I also added some more little features for creating passwords. Hope you like the new update. Comments and suggestions welcome.
    also see Readme.txt file for info on installing and using the script.

    PHP Code:
    <?php
        
    /**
        * @author Ben-Jones
        * @version 1.1
        * @name Random Password Generator Class.
        */ 
         
        
    class TPassGenerator{
            var 
    $i_PwsLength 0;
            var 
    $i_Password "";
            var 
    $i_PwsType 0;
            var 
    $i_PwsCase 0;

            public function 
    __construct($iLength$iType$iCase)
            {
                
    //Set password length to create
                
    $this->i_PwsLength $iLength;
                
    $this->i_PwsType $iType;
                
    $this->i_PwsCase $iCase;
            } 
            
            function 
    GeneratePws()
            {
                
    //Get password length.
                
    $iLen $this->i_PwsLength;
                
    $Alpha "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ";
                
    $Digits "0123456789";
                
                
    //Exit of no length found.
                
    if ($iLen==0) return;
                
                
    //Alpha only.
                
    if($this->i_PwsType==0)
                {
                    
    $patten $Alpha;
                }
                
    //Digit only.
                
    if($this->i_PwsType==1)
                {
                    
    $patten $Digits;   
                }
                
    //Alpha+Digits.
                
    if($this->i_PwsType==2)
                {
                    
    $patten $Alpha .$Digits;
                }
                
                
    //Check for text case.
                
    if($this->i_PwsCase==1)
                {
                 
    //Make lower case.
                 
    $patten strtolower($patten);  
                }
                
                if(
    $this->i_PwsCase==2)
                {
                    
    //Make uppercase.
                    
    $patten strtoupper($patten);
                }
                
                
    //Generate the password.    
                
    for($x=0;$x<$iLen;$x++)
                {
                    
    //Get random number.
                    
    $rnd rand() % strlen($patten);
                    
    //Build string.
                    
    $buff .= $patten[$rnd];
                } 
                return 
    $buff;
            }
            
            function 
    ThePassword()
            {
                
    //Used to return the random password.
                
    return $this->GeneratePws();
            } 
    }
    ?>

    <?php
        
    //Example Script.
        
    $length = (int) $_GET['len'];
        
    $Type = (int) $_GET['type'];
        
    $Upper = (int) $_GET['case'];
        
        
    //Set default length if password is zero.
        
    if($length==0$length 8;
        
        
    //Create password generator class.
        
    $demo = new TPassGenerator($length,$Type,$Upper);
        
        
    //Show random password.
        
    echo "Password is: "."<b>".$demo->ThePassword()."</ b>";
        
    ?>
    Attached Files Attached Files
    Last edited by BenJones; Aug 19th, 2013 at 05:56 PM. Reason: Update

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.