bcart Posted April 15, 2010 Share Posted April 15, 2010 Hi there I am currently writing an exam booking system which seems to be working except for one vital part. This is what happens... A user registers the learners details. At this point I use a Random Alphanumeric Generator to return a unique booking reference which is used at the checkout to bring back all the details of their booking. However, sometimes it doesn't seem to work. When I look at the database I see that (in one example) it has generated the same alphanumeric value which is why at the checkout it returns more than what the person thought they had booked. Here's the code for the generator... function randomPrefix($length) { $random= ""; srand((double)microtime()*1000000); $data = "AbcDE123IJKLMN67QRSTUVWXYZ"; $data .= "aBCdefghijklmn123opq45rs67tuv89wxyz"; $data .= "0FGH45OP89"; for($i = 0; $i < $length; $i++) { $random .= substr($data, (rand()%(strlen($data))), 1); } return $random; } After this has run I assign it to a session variable. Can anyone help? Cheers! Quote Link to comment https://forums.phpfreaks.com/topic/198619-random-alphanumeric-generator/ Share on other sites More sharing options...
efficacious Posted April 15, 2010 Share Posted April 15, 2010 why don't you just use a database and unique ID field? it would auto increment each time a new booking reference was created and the unique id would never get repeated. Quote Link to comment https://forums.phpfreaks.com/topic/198619-random-alphanumeric-generator/#findComment-1042263 Share on other sites More sharing options...
Deoctor Posted April 15, 2010 Share Posted April 15, 2010 try with this <?php for ($i=1;$i<=1000;$i++) { $arr = range('a','z'); $arr1=range('',''); shuffle($arr); echo implode("", $arr);echo "<br>"; } ?> odds of producing the same value are astronomical with this u can also do one more thing try modifying the database to the random value as the primary key.. Quote Link to comment https://forums.phpfreaks.com/topic/198619-random-alphanumeric-generator/#findComment-1042267 Share on other sites More sharing options...
oni-kun Posted April 15, 2010 Share Posted April 15, 2010 Why not use uniqid or something even more random than yours? Guarenteed on a 100,000 record basis there will be a duplicate with your code. Your random generation has a salt of redundancy for collision prevention. function randomPrefix($length) { $random = substr(md5(mt_rand(), false), 0, $length ); } Quote Link to comment https://forums.phpfreaks.com/topic/198619-random-alphanumeric-generator/#findComment-1042291 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.