PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
DeathsMessenger
Forum Newbie
Posts: 20 Joined: Wed Jan 17, 2007 4:08 am
Location: England
Post
by DeathsMessenger » Sat Jan 20, 2007 3:42 am
Code: Select all
<?php
function createRandomPassword()
{
$chars = "abcdefghijkmnopqrstuvwxyz023456789";
srand((double)microtime()*1000000);
$i = 0;
$pass = '' ;
while ($i <= 7) {
$num = rand() % 33;
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}
$password = createRandomPassword();
echo "Your random password is: $password";
?>
theoreticly this should work right?
i can't test it cause my mate changed my server password
Also im tryng to figure out how to randomise it so it loads a certain picture or page
Ollie Saunders
DevNet Master
Posts: 3179 Joined: Tue May 24, 2005 6:01 pm
Location: UK
Post
by Ollie Saunders » Sat Jan 20, 2007 2:10 pm
it would be a whole lot easier to use a substr() of an md5() of some random thing
You don't get a full complement of characters then.
i can't test it cause my mate changed my server password
Hmm you should remedy that. Perhaps install Apache/PHP on your computer. Here's a better version of your function:
Code: Select all
<?php
function getRandomPassword($len = 7)
{
$pass = '';
$chars = '0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]_abcdefghijklmnopqrstuvwxyz';
$lastCharIndex = strlen($chars) - 1;
while ($len--) {
$pass.= $chars[mt_rand(0, $lastCharIndex)];
}
return $pass;
}tested.
DeathsMessenger
Forum Newbie
Posts: 20 Joined: Wed Jan 17, 2007 4:08 am
Location: England
Post
by DeathsMessenger » Sun Jan 21, 2007 2:24 am
thanks i was trying to get it right for ages.
And ole? Installing it now.