PHP

 Home  Computers & Internet  Web Programming PHP
What is PHP?
Echo
Comments
Variables
Constants
Data Types
number_format()
Character Strings
Mathematical Operators
Comparison Operators
Logical Operators
Joining Strings
explode()
implode()
strtolower()
strtoupper()
strlen()
ucfirst()
ucwords()
strrev()
str_replace()
str_repeat()
trim()
strip_tags()
addslashes()
stripslashes()
strpos()
strrpos()
nl2br()
isset()
unset()
empty()
POST
GET
If Statements
If Else Statements
Elseif Statements
Switch Statements
For Loops
While Loops
Do While Loops
Foreach Loops
File Create
File Open
File Read
File Write
File Delete
fgets()
file_get_contents()
Date & Time
$_SERVER
Sessions
Cookies
Arrays

HOW TO GENERATE RANDOM PASSWORDS WITH PHP

This tutorial shows you a simple way to generate random passwords on your site using the pure power of PHP.

First decide what password length you will need:

I want a length of 10 characters so I create a variable called $length and give it a value of 10:

$length =10;

Secondly, I decide from what pool of characters I want my passwords to be composed of:

I create a variable called $characters_to_use and give it a value of abcdef1234567890

You can add, change or edit this to your own satisfaction.

$characters_to_use ="abcdef1234567890";

Thirdly, use PHP's in-built mt_rand() function and a for loop to build the password to specified length as follows:

for ($i = 0; $i < $length; $i++)
{
$do = mt_rand(0,strlen($characters_to_use)-1);
$password = $password . $characters_to_use{$do};
}

Finally, variable $password now contains a random string. Echo it or use it further in your application.

echo $password;

The full script is below:

<?
$length =10;
$characters_to_use ="abcdef1234567890";

for($i = 0; $i < $length; $i++)
{
$do = mt_rand(0,strlen($characters_to_use)-1);
$password = $password . $characters_to_use{$do};
}

echo $password;

?>

Home | Privacy Policy | Terms Of Use | Contact Us