Warning: include() [function.include]: open_basedir restriction in effect. File(/home/toknowmo/public_html/configs.php) is not within the allowed path(s): (/home/migrate.a2/web:/home/ezee4040:/usr/lib/php:/usr/local/lib/php:/tmp) in /home/ezee4040/public_html/toknowmore.net/e/1/php/how-to-generate-random-passwords-with-php.php on line 2

Warning: include(/home/toknowmo/public_html/configs.php) [function.include]: failed to open stream: Operation not permitted in /home/ezee4040/public_html/toknowmore.net/e/1/php/how-to-generate-random-passwords-with-php.php on line 2

Warning: include() [function.include]: Failed opening '/home/toknowmo/public_html/configs.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/ezee4040/public_html/toknowmore.net/e/1/php/how-to-generate-random-passwords-with-php.php on line 2
PHP: How To Generate Random Passwords On The Fly

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;

?>