Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/198619-random-alphanumeric-generator/
Share on other sites

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 :P

 

u can also do one more thing try modifying the database to the random value as the primary key..

 

 

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 );
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.