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
Printable View
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
<?
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);
}
?>
--------------------------------------------------
<?php/** * The letter l (lowercase L) and the number 1 * have been removed, as they can be mistaken * for each other. */function createRandomPassword() { $chars = "abcdefghijkmnopqrstuvwxyz023456789"; srand((double)microtime()*1000000); $i = 0; $pass = '' ; while ($i <= 7) { $num = rand() % 33; $tmp = substr($chars, $num, 1); $pass = $pass . $tmp; $i++; } return $pass;}// Usage$password = createRandomPassword();echo "Your random password is: $password";?>
------------------------------------------------
<?php
// 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
or simply use this
$pass = md5(uniqid(rand(), true));