Results 1 to 3 of 3

Thread: Generate random password Using PHP

  1. #1
    Contributing Member
    Join Date
    May 2006
    Answers
    71

    Generate random password Using PHP

    Hi,
    I want to know how to generate random password in PHP. One of the ways of achieving this is to write a function for the same. I know that there are many methods of doing this. Can someone provide some other methods of doing that?

    Regards,
    Fred


  2. #2
    Contributing Member
    Join Date
    Sep 2006
    Answers
    962

    Smile Re: Generate random password Using PHP

    function Random_Password($length) {
    srand(date("s"));
    $possible_charactors = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $string = "";
    while(strlen($string)<$length) {
    $string .= substr($possible_charactors, rand()%(strlen($possible_charactors))),1);
    }
    return($string);
    }
    echo Random_Password(8);
    ?>
    -------------------------------------------
    function genpassword($length){

    srand((double)microtime()*1000000);

    $vowels = array("a", "e", "i", "o", "u");
    $cons = array("b", "c", "d", "g", "h", "j", "k", "l", "m", "n", "p", "r", "s", "t", "u", "v", "w", "tr",
    "cr", "br", "fr", "th", "dr", "ch", "ph", "wr", "st", "sp", "sw", "pr", "sl", "cl");

    $num_vowels = count($vowels);
    $num_cons = count($cons);

    for($i = 0; $i < $length; $i++){
    $password .= $cons[rand(0, $num_cons - 1)] . $vowels[rand(0, $num_vowels - 1)];
    }

    return substr($password, 0, $length);
    }
    ?>
    --------------------------------------------------

    ------------------------------------------------
    // Generate Random Password
    // ------------------
    // MODES
    // 1 - (n)lowercase
    // 2 - (n)(lowercase + numbers)
    // 3 - (n)(lowercase + uppercase + numbers)
    // 4 - (n)lowercase + (n)numbers
    // 5 - (n)numbers only
    // ------------------
    function passgen($len=4,$mode=4) {
      $chars=array();
      $chars2=array();
      if ($mode > 1){
        // add numbers to $chars
        for($i=48;$i<=57;$i++) {
          array_push($chars, chr($i));
        }
      }
      if ($mode==3){
        // add uppercase to $chars
        for($i=65;$i<=90;$i++) {
          array_push($chars, chr($i));
        }
      }
      if ($mode > 3){
        // add lowercase to $chars2
        for($i=97;$i<=122;$i++) {
          array_push($chars2, chr($i));
        }
      }else{

        // add lowercase to $chars
        for($i=97;$i<=122;$i++) {
          array_push($chars, chr($i));
        }
      }
      if ($mode==4){
        //build first half of password from $chars2 (lowercase)
        for($i=0;$i<$len;$i++) {
          mt_srand((double)microtime()*1000000);
          $passwd.=$chars2[mt_rand(0,(count($chars2)-1))];
        }
        //build second half of password from $chars (numbers)
        for($i=0;$i<$len;$i++) {
          mt_srand((double)microtime()*1037800);
          $passwd.=$chars[mt_rand(0,(count($chars)-1))];
        }
      }else{
        // build password from $chars
        for($i=0;$i<$len;$i++) {
          mt_srand((double)microtime()*1000000);
          $passwd.=$chars[mt_rand(0,(count($chars)-1))];
        }
      }
      return $passwd;
    }
    ?>
    -----------------------------------------

    These are all i got from google search......

    -----------------------
    suresh


  3. #3
    Junior Member
    Join Date
    Jan 2007
    Answers
    3

    Re: Generate random password Using PHP

    or simply use this

    $pass = md5(uniqid(rand(), true));


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact