# Web Development > PHP Generate random password Using PHP

## fred

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

----------


## psuresh1982

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

----------


## ravi_shekhar80

or simply use this

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

----------

