PsychoticDude85
09-23-2006, 02:38 AM
Wrote this for generating passwords and random strings for password reset emails etc, very easy to use. :)
<?php
function strrand($length,$chars='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
{
// Required Variables
$string = '';
// Loop
for($i = 0; $i <= $length-1; $i++)
$string .= $chars[rand(0,strlen($chars)-1)];
// Return our random string.
return $string;
}
?>
Some examples of implementation:
<?php
echo strrand(40); // Outputs 40 random characters.
echo strrand(20); // Outputs 20 random characters.
echo strrand(30,'abcdefghijklmnopqrstuvwxyz0123456789'); // Outputs 30 characters from just lower-case letters and 0-9
?>
<?php
function strrand($length,$chars='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
{
// Required Variables
$string = '';
// Loop
for($i = 0; $i <= $length-1; $i++)
$string .= $chars[rand(0,strlen($chars)-1)];
// Return our random string.
return $string;
}
?>
Some examples of implementation:
<?php
echo strrand(40); // Outputs 40 random characters.
echo strrand(20); // Outputs 20 random characters.
echo strrand(30,'abcdefghijklmnopqrstuvwxyz0123456789'); // Outputs 30 characters from just lower-case letters and 0-9
?>