Random Password Generation

Random Password GenerationA common feature found on many websites upon user registration are random passwords. This tutorial will walk you through a script that generates a random password to a given length.

For this script we will be creating a PHP function to generate the code. I will not discuss functions in great detail here and so I will presume you know pretty much what they are and how and why they are useful. Put simply, using a function to do the work you can use it from anywhere in your application and therefore do not need to rewrite code. I will also presume you know where to put the function (or indeed you have your own preference as to where you put your functions for use). If you want to read up on functions in more detail before continuing the PHP manual should point you in a good direction.

function randomPassword($length, $allow = "abcdefghijklmnopqrstuvwxyz0123456789") {

Anyway, onto the code. We've named our function randomPassword. You can of course name it whatever you like relevant to your purpose, just remember to change all references to it in the code! The function has two parameters - $length and $allow. The $length parameter is simply the length of the password you wish to generate. The $allow parameter is optional. This sets what characters you wish to generate the password from. By default we have set it to generate from lower case letters and numbers 0 - 9.

    $i = 1;
    while ($i <= $length) {

Next up, we will create a loop that loops the number of times you have set in the $length parameter. Therefore in this loop we will generate 1 random character and add it to the final random password...

        $max  = strlen($allow)-1;
        $num  = rand(0, $max);

The above code generates a random number between 0 and the number of characters passed in the $allow parameter minus 1. So if there are 20 characters a random number is generated between 0 and 19 - this is because the substr() function used in the next code references the first character of a string as 0 rather than 1.

        $temp = substr($allow, $num, 1);
        $ret  = $ret . $temp;

Above, a random character is chosen from the $allow parameter using the substr() function. This character is then added onto the $ret variable which will be our random password.

        $i++;
    }
    return $ret;   
}

The code above ends the loop and returns the generated password. Pretty basic stuff to be honest. Our function is now complete! All that is left to do now is run the function in our code to generate a password where we need to. This is done simply like so:

$password = randomPassword(10);

The above example will generate a random password 10 characters long. Here's another example.

$password = randomPassword(10, '0123456789');

The above example will generate a random password 10 characters long and made up of only numbers.

Hope this tutorial has helped! To finish off, here's the full function listing:

function randomPassword($length, $allow = "abcdefghijklmnopqrstuvwxyz0123456789") {

    $i = 1;
    while ($i <= $length) {
   
        $max  = strlen($allow)-1;
        $num  = rand(0, $max);
        $temp = substr($allow, $num, 1);
        $ret  = $ret . $temp;
        $i++;
    }
    return $ret;
   
}

Related Articles

  • Random Password Generator
    Random password generator.
  • Randomizer for ASP
    This article will show you how to create a simple but powerful randomizer.
  • Random Quotes
    Learn how to make Random Quotes using the power of PHP.
    Many people email wondering how the heck we make the random quotes on the title bar at the main page. Well, were finally releasing the secret, and its not as hard as you think :) Lets write the code to pick a random quote using PHP.
  • Password Protect
    Add this login form to a page.
  • Password Protection
    Although there are many uses for a .htaccess file, by far the most popular, and probably most useful, is being able to reliably password protect directories on websites. Although JavaScript can also be used to do this, only .htaccess has total security, because someone must know the password to get ...
  • Database Connection Strings
    Although our ASP database tutorials all use MS Access connection strings, there are many sorts of database software out there for use with ASP. This tutorial will show you how to connect with them...
  • JavaScript Password Protection
    OK, I finally got around to writing the javascript password protection tutorial. You will see the example script here is pretty straightforward, but it is also pretty easy to get around. Ill tell you why and give you links to more secure scripts later in this tutorial.
  • Random Images
    Ever wanted to make links, or banners appear on your site in a random order? Well heres a simple script that will do just that.
  • What's Best: Blogging Traffic or SEO Traffic Generation?
    Ive heard it said, and even seen it written, that blogging is better than SEO for getting traffic. Are blogging traffic or SEO traffic generation techniques better for you, or does it not really matter?
  • How to Make Money Online With No Website and Product (Part 1)
    Read on and you will find your one simple strategy that is going to explode your online earning, even if you do not own your website and product.

Contact Web Design Outsource and get started today

Need Website Designing, Development, Redesigning, Maintenance and SEO services or help growing your company's web presence? Request a free Quote Now.