PHP Snippet - Simple Random Password Script

Posted on June 20, 2011 | Snippets

//set length of password
$length = 12;
function randompass($length)
 {
    
     $chars = "1234567890abcdefGHIJKLMNOPQRSTUVWxyzABCDEFghijklmnopqrstuvwXYZ1234567890";
    
     $thepassword = '';
    
     for($i=0;$i<$length;$i++)
    
     {
    
      $thepassword .= $chars{rand() % 39};
    
     }
 return $thepassword;
 }   
//to use the function
$password=randompass($length);
echo "<strong>Your $length character password is: </strong>".$password";