Random Password Generator

Random Password GeneratorThis tutorial I will be showing you how to create a random password using nothing but loops and random letters. For extra security I will define a minimum length and a maximum length, I will also make it so you can't define a password from using the url.

First we need to define everything. This includes the min length, max length and emptying the password:

Code:

$min=4;
$max=15;
$pwd="";

Now we need to start the loop of creating it, using the $min and $max we can get a random length:

Code:

for($i=0;$i<rand($min,$max);$i++)></rand($min,$max);$i++)>

We need to get a load of letters now, but we have to be sure there are valid letters and not other characters:

Code:

$num=rand(48,122);
if(($num > 97 && $num < 122))
{
$pwd.=chr($num);
}

else if(($num > 65 && $num < 90))
{
$pwd.=chr($num);
}

else if(($num >48 && $num < 57))
{
$pwd.=chr($num);
}

else if($num==95)
{
$pwd.=chr($num);
}

Just a load of checking, nothing much really there. Because this is a tutorial, we would output the result of our password:

Code:

echo $pwd;

Be carefull about including that line for the final script. If you are emailing the password then you don't need it, you decide.

The complete code is here:

Code:

<?php
// filename "pword.php"


$min=4; // minimum length of password
$max=15; // maximum length of password
$pwd=""; // to store generated password

for($i=0;$i<rand($min,$max);$i++)
{
$num=rand(48,122);

if(($num > 97 && $num < 122))
{
$pwd.=chr($num);
}

else if(($num > 65 && $num < 90))
{
$pwd.=chr($num);
}

else if(($num >48 && $num < 57))
{
$pwd.=chr($num);
}

else if($num==95)
{
$pwd.=chr($num);
}

else
{
$i--;
}
}

echo $pwd; // prints the password
?>

Related Articles

  • Random Password Generation
    Web programmers will enjoy this article. It is a quick and effective way to create Random Password Generation - needed for security and anti-spam measures. The author provides concise lines of code, screenshots and simply read texts.
  • 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 ...
  • The Advantages of Dynamic Website Content
    Think about your own surfing behavior. What types of websites do you visit the most often; which ones keep drawing you back? If you are like most internet surfers, you will spend much of your time hanging around websites with dynamic website content, or content that is updated constantly...
  • 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.
  • Introduction to Htaccess
    What is
  • Creating Dynamic Website Content with PHP - MySQL
    Did you know that constantly generating and posting fresh website content is a good way to get return visits from your customers? Heres how its done using PHP.

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.