I have a code that causes the above error to show up repeatedly in my error log, how can I correct it?
public function generate_guid() {
//you can change the length of the autogenerated guid here
//i choose 4 because with 26 possible characters that still gives 456.976 possibilities, if you include numbers ( add 0123456789) to the possible characters you will get 1.679.616 combinations
$length = 4;
//charachters used for string generation
$characters = 'abcdefghijklmnopqrstuvwxyz';
$string = '';
for ($p = 0; $p < $length; $p++) {
$string .= $characters[mt_rand(0, strlen($characters))]; <<-- this is line 92
}
return $string;
}
The problem is that
mt_rand(0, strlen($characters))will generate numbers up to the strings length – but as the string offsets start with 0 the maximum offset is the length minus one. So correct would bemt_rand(0, strlen($characters) - 1).By the way, I would recommend using a characters array generated by
range('a', 'z')(so you don’t have to type it out) and getting an element usingarray_rand.