• 0

help with script


Question

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

  • 0

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

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.