Random string help!
Posted: Wed Jul 10, 2002 9:51 pm
I want to create a random srting as a password,which function I can use?
sorry for english!
sorry for english!
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
$string = rand(1,1000)
$string = md5($string);Code: Select all
$pass = substr(md5(rand(1,1000)),0,8);Code: Select all
<?php // This creates a random password in the form KEYWORD#### (where #### is a 4-digit number)
// Enter a long list of keywords here:
$keywords = array('some_keyword', 'foo', 'hello', 'world', 'php', 'lemonpony', 'gnu2php');
$num_keywords = count($keywords);
// Options:
$MIN_PW = 1000;
$MAX_PW = 9999;
function make_seed() // Got this from http://www.php.net/manual/en/function.srand.php
{
list($usec, $sec) = explode(' ', microtime());
return (float) $sec + ((float) $usec * 100000);
}
srand(make_seed());
// Generate the password
$my_password = $keywordsїrand(0, ($num_keywords - 1))].rand($MIN_PW, $MAX_PW);
print "The password is <b>$my_password</b>";
?>Code: Select all
<?php
function update_user_password ()
{
$seed = (integer) md5(time());
mt_srand($seed);
$password = mt_rand(1,99999999);
$password = substr(md5($password), mt_rand(0, 19), mt_rand(6, 12));
return $password;
}
?>