Results 1 to 3 of 3

Thread: Random Password Generator.

  1. #1

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

    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
    629

    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

  3. #3

    Re: Random Password Generator.

    also one:

    Code:
    <?php 
    # Name: passwords.php 
    # File Description: generates a list of "random" passwords that meet the requirements filled out in teh form 
    # Author: ricocheting 
    # Web: http://www.ricocheting.com/ 
    # Update: 2011-04-29 
    # Version: 1.0.0 
    # Copyright 2011 ricocheting.com 
    
    
    /* 
        This program is free software: you can redistribute it and/or modify 
        it under the terms of the GNU General Public License as published by 
        the Free Software Foundation, either version 3 of the License, or 
        (at your option) any later version. 
    
        This program is distributed in the hope that it will be useful, 
        but WITHOUT ANY WARRANTY; without even the implied warranty of 
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
        GNU General Public License for more details. 
    
        You should have received a copy of the GNU General Public License 
        along with this program.  If not, see <http://www.gnu.org/licenses/>. 
    */ 
    
    
    
    $length=8;//default password length 
    $count=10;//default number of passwords 
    
        $chk_upper=!empty($_GET['chk_upper']); 
        $chk_lower=!empty($_GET['chk_lower']); 
        $chk_digit=!empty($_GET['chk_digit']); 
        $chk_special=!empty($_GET['chk_special']); 
    
    
    // if no char sets selected: default 
    if(!$chk_upper && !$chk_lower && !$chk_digit && !$chk_special){ 
        $chk_safe=true; 
        $chk_upper=true; 
        $chk_lower=true; 
        $chk_digit=true; 
        $chk_special=false; 
    
        $chk_require=true; 
        $chk_startdigit=true; 
    } 
    // use what was selected by user 
    else{ 
        $chk_safe=!empty($_GET['chk_safe']); 
        $chk_require=!empty($_GET['chk_require']); 
    
        $length=(!empty($_GET['length'])?(int)$_GET['length']:$length); 
            if($length>20)$length=20;// capped at 20 chars max length 
            if($length<4)$length=4;// capped at 4 chars min length 
    
        $count=(!empty($_GET['count'])?(int)$_GET['count']:$count); 
            if($count>99)$count=99;// capped at 99 passwords max 
            if($count<1)$count=1;// capped at 1 password min 
    } 
    
         
        $str_upper=''; 
        $str_lower=''; 
        $str_digit=''; 
        $str_special=''; 
    
    // characters that will be used 
    if($chk_upper==1){ 
        $str_upper="ABCDEFGHJKLMNPQRSTUVWXYZ"; 
        if($chk_safe!=1) $str_upper.="IO"; 
    } 
    
    if($chk_lower==1){ 
        $str_lower.="abcdefghjkmnpqrstuvwxyz"; 
        if($chk_safe!=1) $str_lower.="iol"; 
    } 
    
    if($chk_digit==1){ 
        $str_digit.="23456789"; 
        if($chk_safe!=1) $str_digit.="01"; 
    } 
    
    if($chk_special==1){ 
        $str_special.="~!@#$%^&*()_+[]\;',./~{}|:\"<>?"; 
    } 
    
    
    $use=$str_upper.$str_lower.$str_digit.$str_special; 
    $use_length=strlen($use)-1; 
    
    echo '<pre>'; 
    
    // number of passwords to create 
    for($x=0; $x<$count; ++$x){ 
        $password=''; 
    
        // if requiring one of each type, preload the password with those. is randomized later with str_shuffle() 
        if($chk_require==1){ 
            if($chk_upper==1) $password.=$str_upper[rand(0,(strlen($str_upper)-1))]; 
            if($chk_lower==1) $password.=$str_lower[rand(0,(strlen($str_lower)-1))]; 
            if($chk_digit==1) $password.=$str_digit[rand(0,(strlen($str_digit)-1))]; 
            if($chk_special==1) $password.=$str_special[rand(0,(strlen($str_special)-1))]; 
        } 
    
        // length to make password (minus the characters already forced by require) 
        for($i=strlen($password); $i<$length; ++$i){ 
            $password.=$use[rand(0,$use_length)]; 
        } 
    
        // shuffle the char order then print (with any special chars html safe) 
        echo htmlentities(str_shuffle($password))."\n"; 
    } 
    echo '</pre>'; 
    
    ?> 
    
    
    <form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="get"> 
    <input type="checkbox" name="chk_safe"<?php echo ($chk_safe?' checked="checked"':''); ?>> Hide confusing characters<br /> 
    <input type="checkbox" name="chk_upper"<?php echo ($chk_upper?' checked="checked"':''); ?>> Allow uppercase characters<br /> 
    <input type="checkbox" name="chk_lower"<?php echo ($chk_lower?' checked="checked"':''); ?>> Allow lowercase characters<br /> 
    <input type="checkbox" name="chk_digit"<?php echo ($chk_digit?' checked="checked"':''); ?>> Allow digits<br /> 
    <input type="checkbox" name="chk_special"<?php echo ($chk_special?' checked="checked"':''); ?>> Allow special characters<br /> 
    
    <input type="checkbox" name="chk_require"<?php echo ($chk_require?' checked="checked"':''); ?>> Require at least one character of each chosen type<br /> 
    
    <input type="text" name="length" value="<?php echo $length;?>" size="2" maxlength="3"> Password length<br /> 
    <input type="text" name="count" value="<?php echo $count;?>" size="2" maxlength="3"> How many passwords<br /> 
    
    <input type="submit" value="Create"> 
    </form>
    Last edited by bestellen; Sep 15th, 2015 at 01:38 PM.

Posting Permissions

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



Click Here to Expand Forum to Full Width