Project: Password generator

Returning to our example of password generation, here’s how a password generation function might look:

function generatePassword($length) {
   $validPasswordCharacters = 'abcdefghijklmnopqrstuvwxyz0123456789';
   $myPassword = ‘’;
   for ($n = 1; $n < $length; $n++) {
      $whichCharacter = rand(0, strlen($validPasswordCharacters)-1);
      $myPassword .= $validPasswordCharacters{$whichCharacter};
   return $myPassword;
   echo ‘This will never be echoed’;

This is by far the most complex PHP we’ve looked at. We’ll break it down one line at a time.

Line 1. The keyword function tells PHP we’re declaring a function. The function name is generatePassword, and it can take a parameter that we’ll use to control the length of the password.

Line 2. We create a new variable called $validPasswordCharacters and containing all the characters that are allowed in our password.

Line 3. We create a new variable called $myPassword, and set it to be an empty string for now.

Line 4. This for loop should look familiar to you: we used loops like this in the Arrays session. Here we’ve limited our for loop by using the $length parameter

Line 5. We create a new variable called $whichCharacter and set it to contain the result of another function called rand(). Rand() generates a number between 0 and the length of the $validPasswordCharacters string −1.

Line 6. We add a single character to the end of $myPassword. The character is retrieved by just getting the character at a single position in $validPasswordCharacters. Line 5 provided us with a random number that is within the range we are after.

Line 8. Finally we return the value found. The keyword return will prevent any further lines from running: line 9 will never execute. Comment out or delete line 9.

Here’s how we’ll call our script:

$myPassword = generatePassword(12);
echo $myPassword;

If you put the function and and this call script in a single php file you can generate a new password on each page load. If you hit refresh on your browser it will generate a new password.

Where next?

Now try to extend the function. Here are some improvements that could make it much more useful:

1. A more complex list of valid characters, including upper case and non-alphanumeric characters.

2. Generate 3 passwords for each page load, with 6, 8, and 12 characters

3. Future improvements should also consider that it is good practise to enforce a minimum number of non-alphanumeric, uppercase, and numeric characters. In other words ensure it creates enough of each kind to be secure.

I hope you’ve found this tutorial useful. Please leave a comment or use the contact form if you have any questions.
Have fun!

This post is part of PHP First Steps Tutorial.

24 hr tutorial Hey!
Based on the feedback from readers of this tutorial series I’ve expanded, revised, and improved this brief intro as a whole book. It is much longer, so you get a more in-depth guide to getting started. As a result you’ll write better code. When you buy the book you also get a download password for all the source code from the tutorials. Build a functioning blog from scratch, and other useful mini-projects! Check it out »

No Comments

Be the first to start the conversation.

Leave a Reply


Text formatting is available via select HTML.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>