mikey Posted April 4, 2003 Share Posted April 4, 2003 i have this random password denerator script: <?php global $rand_pass; $deck = array_merge(range("0", "9"),range("a", "z"),range("A", "Z")); for($i = 0; $i < 6; $i++){ $shuffle = rand(0,sizeof($deck)); $rand_pass .= $deck[$shuffle]; } echo "$rand_pass"; ?> it works ok.. but every now and again (randomly) it gives an error saying: Notice: Undefined offset: 71 in C:\Program Files\Apache2\htdocs\up\pass_gen.php on line 8 Does anyone have any idea why this happends, and how i could fix it? Thanx :) Link to comment Share on other sites More sharing options...
0 pringlex Posted April 5, 2003 Share Posted April 5, 2003 The problem lies in sizeof(), here's why: There are 62 elements in the array $deck, and the sizeof() function will also return this figure. However - the elements are numbered from 0 to 61. So when the rand() function returns the maximum value given to it - in this case 62 - the '$rand_pass .= $deck[$shuffle];' line tries to get the 63rd element, and then returns the error because the 63rd element doesn't exist. By changing $shuffle = rand(0,sizeof($deck)); to $shuffle = rand(0,(sizeof($deck)-1)); you'll eliminate the problem. Hope this helps! Link to comment Share on other sites More sharing options...
0 mikey Posted April 5, 2003 Author Share Posted April 5, 2003 yea, it worked, cheers m8!!! Link to comment Share on other sites More sharing options...
0 AshMan Posted April 5, 2003 Share Posted April 5, 2003 OK, from the results in $rand_pass, how can you prevent the following from appearing: 1, 2, 9, 0, g, i, l, o, z, I, l, O and Z Link to comment Share on other sites More sharing options...
Question
mikey
i have this random password denerator script:
<?php global $rand_pass; $deck = array_merge(range("0", "9"),range("a", "z"),range("A", "Z")); for($i = 0; $i < 6; $i++){ $shuffle = rand(0,sizeof($deck)); $rand_pass .= $deck[$shuffle]; } echo "$rand_pass"; ?>it works ok.. but every now and again (randomly) it gives an error saying:
Notice: Undefined offset: 71 in C:\Program Files\Apache2\htdocs\up\pass_gen.php on line 8
Does anyone have any idea why this happends, and how i could fix it?
Thanx :)
Link to comment
Share on other sites
3 answers to this question
Recommended Posts