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