Zend - The PHP Company




Passwords

Add Code


Random Password Generator  

Type: code fragment
Added by: nickuk
Entered: 08/02/2003
Last modified: 01/11/2007
Rating: *** (4 votes)
Views: 6339
a simple function that can generate a random password/verification code to an unlimited number of chars by modifying make_password(int).


<? 
// make the seed for the random generator 
function make_seed()
{
    list(
$usec$sec) = explode(' 'microtime());
  return (float) 
$sec + ((float) $usec 100000); 


// make the password 
function make_password($pass_len

  
//seed the random generator 
    
mt_srand(make_seed()); 
  
//create password 
    
$password ""
    for (
$loop 0$loop $pass_len$loop++) 
    { 
        switch(
mt_rand(02))
        { 
            case 
0$password .= mt_rand(09);            break; // Number (0-9) 
            
case 1$password .= chr(mt_rand(97122));    break; // Alpha Lower (a-z) 
            
case 2$password .= chr(mt_rand(6590));    break; // Alpha Upper (A-Z) 
        
}
    }
  return 
$password;
}

$password make_password(7);
print(
"Random Password: $password<br>n"); 

?> 


Usage Example


See the example


Rate This Script





Search



This Category All Categories