PHP is open source scripting language. It\'s widely used to develop web applications.  Home Web Programming PHP Random Password Generator
Your Ad Here

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
?>


Author's URL: NuPixel
Thank you for voting.
Rate this Materials:
Bad 
1 2 3 4 5 Excellent
print this page subscribe to newsletter subscribe to rss

Web programming � everything from the basics of visual design and architecture to the specifics of applications, graphics, and scripting. More Web Programming: Most Popular Materials | Fresh Materials | More PHP Tutorials at LearnPHP.org

Add comments to "Random Password Generator"

Only registered users can write comment

No comments yet...