Filed Under (PHP Code Samples) by Srikanta on May-30-2007

This random password generator generates the password upto a desired length and includes the desired set of characters. It ensures that you will get atleast one random character from the character type you have choosen.

Click here to see the demo of the Random Password Generator

Code (php)

<?
$Source[0] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$Source[1] = "0123456789";
$Source[2] = "!@#$%^*_-+=?|:";
if($_POST['submit'] == 1)
{
        if($_POST['alpha'] == '' && $_POST['num'] == '' && $_POST['other'] == '')
        {
                $Error = "Please provide 'Password Type'.";
        }
        else
        {
                if($_POST['alpha'] == '1' )
                {
                        $Use[] = $Source[0];
                }
                if($_POST['num'] == '1' )
                {
                        $Use[] = $Source[1];
                }
                if($_POST['other'] == '1' )
                {
                        $Use[] = $Source[2];
                }
                $Passwordlen = $_POST['passwordlen'] ;
                if($Passwordlen >= count($Use))
                {
                        $Min = 0;
                        while($i < $Passwordlen)
                        {
                                $Max = strlen($Use[$i % count($Use)])-1;
                                $Rand = rand($Min,$Max);
                                $Password .= substr($Use[$i % count($Use)],$Rand,1);
                                $i++;
                        }
                }
                else
                {
                        $Error="Please enter password length  greater than number of 'Password Type' selected.";
                }
        }
}
?>
<html>
<head>
<style>
<!--
.box{
        background-color:#ba0808;
        color:#ffdf9a;
        font-size:20px;
}
.header{
        background:#6f0006;
        color:#ffa200;
}
.password{
        background:#630102;
        color:#ffa200;
}
-->
</style>

</head>
<body>
<CENTER>
<div class="box">
<form method="POST">
<div class="header">
<h1>Password Generator</h1>
</div>
<table class="box">
<?
if($Error != '')
{
?>
<tr>
<td>
                <font color="#ffa200" size="6"><?=$Error;?></font>
        </td>
</tr>

<?
}
?>
<tr>
<td>
                Enter your password length:
<input type="text" name="passwordlen" size="2" maxlength="2" value="<?=$_POST['passwordlen'];?>">
</td>
</tr>
<tr>
<td>
                Please Select Characters to be used in the password:
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="alpha" value="1" <?
if($_POST['alpha']!='')
{
        echo "checked";
}
?>
>Use Alphabets (e.g., QlqzkBKrod )
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="num" value="1" <?
if($_POST['num']!='')
{
echo "checked";
}
?>
>Use Numbers (e.g., 5122401523 )
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="other" value="1" <?
if($_POST['other']!='')
{
echo "checked";
}
?>
>Use Other Characters (e.g., ?=:!?%^-@|  )
</td>
</tr>
<tr>
<td>
<input type="hidden" name="submit" value="1">
<input type="submit" value="Generate">
        </td>
</tr>

</TABLE>
</form>
<div class="password">
<?
if($Password != '')
{
?>
<h2>Generated Password: <?=$Password;?></h2>

<?
}
else if($Error != '')
{
?>
<h2>Error : <?=$Error?></h2>

<?
}
else
{
?>
<h3>Password Generater</h3>

<?
}
?>
</div>
</div>

</CENTER>
</body>
</html>
 



Post a comment
Name: 
Email: 
URL: 
Comments: