In this tutorial IŽll show you how to make a simple php script that will display random password every time you click "Generate Password". Click here , to see how script works.
Step 1: To realize this we will need a function with 2 parameters: $passlength and $chr.The $passlength is length of the password you wish to generate and $chr sets what characters you wish to generate. By default script can generate abcdefghijklmnopqrstuvwxyz0123456789_!@#$%&* .
PHP Code:
<?php
/*Free PHP Scripts created by /hscripts.blogspot.com/*/
function random_password_generator($passlength, $chr)
{
for($i = 1;$i <= $passlength;$i++) /*This loop will generate 1 random character
and add it to the final random password*/
{
$maximun = strlen($chr)-1;/*The strlen() function returns the length of a string.*/
$number = rand(0, $maximun);/*The rand() function generates a random integer*/
$t = substr($chr, $number, 1);/*The substr() function returns a part of a string*/
$gen = $gen . $t; /*$Get variable is our random password.*/
}
return $gen;
}
?>
Here is an html page with table and simple form where visitor can click on Generate Password button. In this phase we need to run function "random_password_generator " . For example random_password_generator(6,"abcd"); This example will generate password , that is 6 charachters long and and made up of "abcd". The HTML code is quite simple,I'll not discuss details about html code ,I'll presume that you know how to create html table and etc. Finnaly , here is the full code listing:
<html>
<head>
<title>random password generator</title>
</head>
<body>
<?php
/*Free PHP Scripts created by /hscripts.blogspot.com/*/
function random_password_generator($passlength, $chr)
{
for($i = 1;$i <= $passlength;$i++) /*This loop will generate 1 random character
and add it to the final random password*/
{
$maximun = strlen($chr)-1;/*The strlen() function returns the length of a string.*/
$number = rand(0, $maximun);/*The rand() function generates a random integer*/
$t = substr($chr, $number, 1);/*The substr() function returns a part of a string*/
$gen = $gen . $t;/*$Get variable is our random password.*/
}
return $gen;
}
?>
<table border="3px" width="15%" bordercolor="#8080cc" bordercolorlight="#8080c0" bordercolordark="#400080" bgcolor="#ffffcc">
<tr><td align="center">
<form action="randompass.php" method="post" >/* The required action attribute specifies where to send the form-data when a form is submitted., in my example it's randompass.php */
<input type="submit" value="Generate Password" name="submitbutton">
</td></tr>
<tr><td align="center">
<b><font color="#0000FF">
<?php
if(isset($_POST['submitbutton'])){
echo random_password_generator(6,"abcdefghijklmnopqrstuvwxyz0123456789_!@#$%&*");}/* Function random_password_generator return random pasword*/
?>
</font></b>
</td></tr>
<tr><td><marquee behavior="scroll" direction="left" scrollamount="2">Created by hscripts.blogspot.com</marquee></td></tr>
</table>
</body>
<html>
Just copy code above in notepad, save as randompass.php , you can choose another file name, but then you need to change action="randompass.php" to action="yourname.php" and upload file to server, check this cool html codes .
Random Password Script
Started by
blaz1988
, Apr 27 2009 12:53 PM
No replies to this topic
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users