Forum Moderators: coopster

Message Too Old, No Replies

selecting which characters comprise a password

$password = substr(md5(time()),0,6);

         

mylungsarempty

7:32 am on Jan 5, 2004 (gmt 0)

10+ Year Member



how can i alter

$password = substr(md5(time()),0,6);

to randomly create a password using only the letters a-g?

stargeek

10:35 am on Jan 5, 2004 (gmt 0)

10+ Year Member



$letters = array('a', 'b', 'c'...
array_shuffle($letters);
$password = $letters[0].$letters[1].$letters[2].$letters[3].$letters[4].$letters[5];

this is admitely a little cludgy but it would work.

mylungsarempty

12:57 pm on Jan 5, 2004 (gmt 0)

10+ Year Member



the effort is appreciated, but i couldn't get that to work at all.

stargeek

2:46 pm on Jan 5, 2004 (gmt 0)

10+ Year Member



there were a few errors in that example, try this instead:

[code]
<?php
$letters = array('a', 'b', 'c', 'd', 'e', 'f', 'g');
srand((float)microtime() * 1000000);
shuffle($letters);
$password = $letters[0].$letters[1].$letters[2].$letters[3].$letters[4].$letters[5]
?>
[\code]

bcolflesh

2:49 pm on Jan 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Per the man pages, if you're using PHP >= 4.2.0:
php.net/shuffle

"Note: As of PHP 4.2.0, there is no need to seed the random number generator with srand() or mt_srand() as this is now done automatically."

stargeek

3:19 pm on Jan 5, 2004 (gmt 0)

10+ Year Member



I figured that i would incldue the seed line just incase his version is older, i don't think it hurts at all if its a new version.

mylungsarempty

11:47 pm on Jan 6, 2004 (gmt 0)

10+ Year Member



Hey, that worked out great -- thankyou

mylungsarempty

6:01 pm on Jan 15, 2004 (gmt 0)

10+ Year Member



so if this is my current code:

$letters = array('a', 'b', 'c', 'd', 'e', 'f', 'g');
srand((float)microtime() * 1000000);
shuffle($letters);
$password = $letters[0].$letters[1].$letters[2].$letters[3].$letters[4];

how can i alter this so that each letter does not appear only once? By complete randomness, perhaps 'c' could appear three times, and 'd' could appear twice. What code would allow that to occur?

bcolflesh

6:17 pm on Jan 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Put your shuffle in a loop and build the password variable one letter at a time.

mylungsarempty

8:22 pm on Jan 15, 2004 (gmt 0)

10+ Year Member



i don't quite understand what that would look like...

mylungsarempty

5:04 pm on Jan 17, 2004 (gmt 0)

10+ Year Member



the error i get with the following code is: Fatal error: Cannot break/continue 4 levels in /mnt/web_a/d11/s01/b01b9236/www/asdf.php on line 21

<?php
srand((float)microtime() * 1000000);
$letters = array('a', 'b', 'c', 'd', 'e', 'f', 'g');
while ($password = $letters[0]){
shuffle($letters);
$password = $password . $letters[0];
break 4;
echo $password;
}
?>

what am i doing wrong there?

dmorison

5:15 pm on Jan 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's a different approach:


$passseed = substr(md5(time()),0,6);

$password = "";

for($i=0;$i<=5;$i++) $password .= chr(97+(ord(substr($passseed,$i,1))%7));

You only want the letters 'a' thru 'g', so this method selects characters out of the ASCII table starting at 97 ('a'); and adding the result of the ASCII value of the i'th character of your seed password "MOD 7", which gives a value between 0 and 6, thus selecting a random character between 'a' and 'g', inclusive.

That was based on your original "how could I alter" question; however you could always just do this:


$password = "";

for($i=0;$i<=5;$i++) $password .= chr(97+rand(0,6));

...which is just as "random".