Dreamhost

Looking for somewhere to host your site? Browse our PHP web hosts section!

Home > Code Snippets > Create a random password

Create a random password

Description

Nice little function that will generate a completely random password.

The code


<?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$num1);
        
$pass $pass $tmp;
        
$i++;
    }

    return 
$pass;

}

// Usage
$password createRandomPassword();
echo 
"Your random password is: $password";

?> 

Get the code

Download the file to your computer: Click here to get the file