PHP Random Password Generation Tutorial

Ranked #3,604 in Internet, #201,572 overall

Generating random passwords in PHP

In this tutorial we'll be creating a function to generate random passwords ( or strings in general ) with. We'll discuss 2 ways of doing this. In both ways we'll need to setup a charset ( list of characters that may occur in the string ) and provide a length for the password. The charset we'll use in this tutorial is the following 'default' one (containing letters and numbers):



abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
01234567890123456789

The reason for having all numbers 3 times is to make the chance of a number occuring in our password about equal with the chance of a letter occuring in our password.

Now there's 2 methods to use this charset for generating random passwords:

1. Picking characters from the charset with random positions (and so random characters are picked).
2. Shuffling the charset and picking characters at first position (which is different every time the charset is shuffled and one character is picked).

We'll start with creating the structure of our function to generate random passwords with.

Function: randomStr

Creating the random password generating function.

We can already setup the basics for the random password generating function. We'll call the function randomStr as it basicly generates a random string of letters and numbers.

function randomStr( parameters ) {
function code
}

The parameters for the function are the data the funcion needs. In this case our function needs a charset ( the characters to choose randomly from to create the password ) and the length of the password ( the amount of characters to choose randomly ).

function randomStr($length, $charset) {
function code
}

What I usually do for the charset as parameter is to give it a default value. This way the default charset is used when no other charset is provided, but it does offer the posibility to offer an alternative charset.

function randomStr($length, $charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
01234567890123456789") {
function code
}

Now let's write the basic function code. It basicly needs to define a variable $str which will contain the output ( random password ) in the end. We also need a for loop to add random characters to this $str variable for the amount of times as set in $length. And of course the function needed to return the random password created in $str, so we add the line return $str to the end of the function.

function randomStr($length, $charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
01234567890123456789") {
$str = "";
for($i=0;$i<$length;$i++) {
add random character of $charset to $str.
}
return $str;
}


Now all we need to do more is write the code to add a random charater to $str inside of the for loop ( which repeats it $length amount of times ).

Picking a random character (method 1)

Picking a random character by picking a character from a random position.

The first method to pick a random character is to pick one from the charset from a random position. We can use the PHP function mt_rand to generate a random position. We set the min. value for the random number to 0 ( =first position in charset ) and the max. value for the random number to the length of the charset minus 1 ( =last position in charset ).

$pos = mt_rand(0, strlen($charset)-1);

Now we got a random position and so a random character to pick from the charset. All we need to do now is pick that character at that position and add it to $str ( the output random password ).

$pos = mt_rand(0, strlen($charset)-1);
$str .= $charset[$pos];

That's all! We written the code to add a random character to the password ($str) in just 2 lines of code. Now let's put it together with our function . We need to put it inside of the for loop that's repeated $length amount of times.

function randomStr($length, $charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
01234567890123456789") {
$str = "";
for($i=0;$i<$length;$i++) {
$pos = mt_rand(0, strlen($charset)-1);
$str .= $charset[$pos];
}
return $str;
}

Picking a random character (method 2)

Picking a random character by shuffling the charset and picking the first character.

Another way to pick a random character is to shuffle the charset using the PHP function str_shuffle. This way we can pick the first character of the charset as it will be a different one each time we shuffle the charset.

$charset = str_shuffle($charset);
$str .= $charset[0];

This method may even be easier than the previous one, however both just consist of 2 lines of code. We here shuffled $charset and then picked the first character out of it and added it to $str. That's all there's to do. Now we can merge this with the function as well, and we'll have another random password generating function that does exactly the same!

function randomStr($length, $charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
01234567890123456789") {
$str = "";
for($i=0;$i<$length;$i++) {
$charset = str_shuffle($charset);
$str .= $charset[0];
}
return $str;
}

Next PHP Tutorial

Loading poll. Please Wait...

You might also like ...

Loading

Comments & Questions

Got any questions about the tutorial or just want to comment? This is the place to do so.

Related Products (Amazon)

Loading

Related Products (eBay)

Loading

by

webcodez

"Give a man a program and you'll frustrate him for one day. Teach a man to program and you'll frustrate him for a whole lifetime."

webcodez
GameHeroes
more »

Feeling creative? Create a Lens!