Randomisation

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

Post Reply
User avatar
DeathsMessenger
Forum Newbie
Posts: 20
Joined: Wed Jan 17, 2007 4:08 am
Location: England

Randomisation

Post by DeathsMessenger »

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
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

it would be a whole lot easier to use a substr() of an md5() of some random thing
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

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.
User avatar
DeathsMessenger
Forum Newbie
Posts: 20
Joined: Wed Jan 17, 2007 4:08 am
Location: England

Post by DeathsMessenger »

thanks i was trying to get it right for ages.
And ole? Installing it now.
Post Reply