06-11-2008, 10:09 AM
|
#4 (permalink)
|
|
The Frequenter
Join Date: Oct 2007
Location: Manchester, UK
Posts: 469
Thanks: 26
|
Nice script, thanks for sharing.
One thing however, there is a mistake, the script generates a 'undefined offset' because you have set the max value of mt_rand to '62' (number of elements in the array) so every now and again you will get the '62' being produced and used as a key for the array, however the maximum array key value is only '61' (php is zero indexed so the first value is 0 not 1) thus the key '62' doesn't exist.
to rectify just subtract 1 from the max value of mt_rand
PHP Code:
<?php function myRandomStr($passLen=16, $shakes=5) { // Get some letters and integers easy and quick into arrays $smal_letters = range('a','z'); $big_letters = range('A','Z'); $integers = range('0', '9');
// merge thoose arrays above to one array $combined = array_merge($smal_letters, $big_letters, $integers); // shake it around abit for($s = 0; $s < intval($shakes); ++$s) { shuffle($combined); } // find out how big the array is $len = count($combined) - 1; // then finaly create the string for($i = 0; $i < intval($passLen); ++$i) { // Choose some random letters from the array $salt = mt_rand(0, $len); echo $combined[$salt]; } }
// Lets try a 15 char long and with 5 shakes. myRandomStr(15, 5);
that should now run correctly.
apart from that its nice, thanks m8.
__________________
Last edited by sketchMedia : 06-11-2008 at 12:10 PM.
Reason: removed mistake in last loop, see salathe's post below
|
|
|
|