Hi there,

I am looking for a suggestion, and wondered if somebody may be able to help. I have a client, who requests that within his form there is a unique six digit generated number, I am looking for a piece of software or a Dreamweaver extension that may be able to do this but so far I have not found anything on the Internet, I have read that there is a way of doing this using some JavaScript code but if it is possible to do it as a standard PHP code without any MySQL database it would be much appreciated. A software package or extension would be preferable to hand coding if possible.

All the very best for now.
John

Member Avatar for diafol

Six digit and uniqueness sound out of sorts. Shame you don't want to use MySQL because that would give you a clean form with the random number being created in the query.

Check the online php manual for random number generation.

<input type="hidden" value="<?php echo mt_rand(100000, 999999);?>" id="rand" name="rand" />

How's that?

I have a few questions about this. Is the form we are talking about a login form? What is the purpose of this random number?

unique, and random are mutually exclusive
is probably easiest to serialize
create a file, in this case i keep it in cgi-bin, called countforms.bcnt if you change the name change it for all occurrences in the scritp
chmod writeable to the user that php runs under
contents six digits
000000
then

<?php $file = "/cgi-bin/countforms.bcnt";
$form_id = file_get_contents( $file ) + 1;
$fp = fopen($file, "w");
fputs ($fp, "$form_id");
fclose($fp); ?>
<input type='hidden' name='form_id' value='<?php echo $form_id;  ?>'>

maybe not the best way, but one way
or you could have the script only write the updated serial number on form submit so that the number does not increase crazily

unique, and random are mutually exclusive

They may mean different things, but they're not mutually exclusive.

For them to be mutually exclusive would mean that anything unique cannot be random and anything random cannot be unique.

It is actually possible to generate something that is both random and unique (the latter via a test against a history list).

000000
then

<?php $file = "/cgi-bin/countforms.bcnt";
$form_id = file_get_contents( $file ) + 1;
$fp = fopen($file, "w");
fputs ($fp, "$form_id");
fclose($fp); ?>
<input type='hidden' name='form_id' value='<?php echo $form_id;  ?>'>

Your code doesn't preserve the zero-padding. Changing this line:

fputs ($fp, "$form_id");

to this:

fputs ($fp, sprintf("%06d",$form_id));

would fix that.

commented: Thanks the obvious, is so hard to see +4

Thank you for the padding code,
I had in mind before posting, messed up on posting.

It is actually possible to generate something that is both random and unique (the latter via a test against a history list).

then it is no longer random, could roll a 6 on a die a hundred times consecutively.

Hi

Thanks for your help ardav but, when I put the code in to the HTML form I get email back to me the following:

Name: Fred Flintstone
E-mail: Fred@Flintstone.com
rand: <?php echo mt_rand(100000, 999999);?>

This is no is not right, please can you advise where I'm going wrong.

Kind regards.

John

hi to everyone.

All this number will be if they "tracking number" which must be unique to the person who fills in the form, the number must also be six digits. No login is required, and the details on the form itself need to be very simple. All I am looking for is name, address, telephone number, e-mail, tracking number and submit. Hope this information helps.

Member Avatar for diafol
<?php echo mt_rand(100000, 999999);?>

works fine for me. You must have done something wrong. Has your form page got a .htm or .html extension? If so, this is the root of the problem - it should be .php.

Use:

<p><?php echo mt_rand(100000,999999);?></p>

mt_rand has been supported since PHP4. It's quicker than rand().

Although I've supplied this suggestion, I wouldn't use it myself. It's as AlmostBob says - random CAN mean non-unique. I'm sure that you know that you won't roll 899999 instances before you hit a duplicate number - this COULD happen within the first 1000 instances:

In fact, statistical approximation show a 'match probability' of greater than even at 1118 instances.

given that countforms.bcnt is a file in the cg-bin directory chmod writeable contents
000000
outputforms.csv is a file in the cgi-bin directory chmod writeable contents
ID,First name, Lastname,email,address,telephone

<?php if ($_POST['email']) {
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];
$address=$_POST['address'];
$telephone=$_POST['telephone'];
$countfile = "/cgi-bin/countforms.bcnt";
$outfile = "/cgi-bin/outputforms.csv";
$form_id = file_get_contents( $file ) + 1;
$fp = fopen($file, 'w');
fputs ($fp, sprintf('%06d',$form_id));
fclose($fp);
$fo = fopen($outfile,'a+');
fputs ($fo, "$form_id,$firstname,$lastname,$email,$address,$telephone\n");
fclose($fo);
}
else {
$firstname=$lastname=$email=$address=$telephone=" ";
$form_id='id will be generated on submission';
}  ?>
<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post'>
<div>ID= <?php echo $form_id." Please record this number for your records"; ?></div>
Firstname <input type=text name='firstname' value="$firstname" />
Lastname <input type=text name='lastname' value="$lastname" /><br />
Email <input type=text name='email' value="$email" />
Address <input type=text name='address' value="$address" /><br />
telephone <input type=text name='telephone' value="$telephone" /><br />
<input type='submit' name='submit' value='submit' />
</form>

not fully tested, creates a csv file of the submitted forms to be loaded into excell, there are dozens of mail handlers already
the input validation is USELESS, I didnt take any care of it, this is a proof of concept not a 'good' script,

Hi there and thanks

Not thinking I did not put it in a .php page but a .html page. Thank you for your help. I am on the design side making site look pretty, I have some Basic knowledge of PHP but not enough. almostbob, if you could go further into your explanation I would be interested in looking at the way you have implemented the random number. I would like to take this opportunity to thank you both for your help. It is greatly appreciated.

John

<?php if ($_POST['email']) { /* check whether the file is loading from the post and assign values to the variables */
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];
$address=$_POST['address'];
$telephone=$_POST['telephone'];
$countfile = "/cgi-bin/countforms.bcnt"; /* define the file containing the count */
$outfile = "/cgi-bin/outputforms.csv"; /* define the file containing the form data, in this case a .csv text file containing 1 row of each form submitted */
$form_id = file_get_contents( $file ) + 1; /* read the last form number and increment */
$fp = fopen($file, 'w'); /* open the couint file and erase the contents */
fputs ($fp, sprintf('%06d',$form_id));  /* put the new count number to the count file
fclose($fp);
$fo = fopen($outfile,'a+'); /* open the output file to append the new data to the end /*
fputs ($fo, "$form_id,$firstname,$lastname,$email,$address,$telephone\n"); /* write the form to the output file */
fclose($fo);
}
else {
$firstname=$lastname=$email=$address=$telephone=" "; /* blank all data fields */
$form_id='id will be generated on submission'; /* information prompt */
}  ?>
<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post'>
<div>ID= <?php echo $form_id." Please record this number for your records"; ?></div>
Firstname <input type=text name='firstname' value="$firstname" />
Lastname <input type=text name='lastname' value="$lastname" /><br />
Email <input type=text name='email' value="$email" />
Address <input type=text name='address' value="$address" /><br />
telephone <input type=text name='telephone' value="$telephone" /><br />
<?php if (!S_POST['email']) {echo "<input type='submit' name='submit' value='submit' />" ;} else { echo "thank you";  }
</form> <!-- normal html form -->

the form submits to itself, on submission it writes the id number into the id field and displays the data entered in other fields,
I delete the submit button on the post-submit form on this one so that it is not submitted over n over
the number is consecutive
the serial number is a simple file containing a six digit number,
the number is initially set to 000000 when the script is installed
the number is read, incremented and written back to the erased file after the form is validated
in this concept the validation is lousy(nonexistent), twas just to try it out
thanks edwinhermann for the code correction
the new six digit number and the input data are written to the output file and to the browser for the user to read
the output file is .csv comma separated values, the initial content of the file is a comma separated list of the field names
the data fields are written comma separated
excell reads csv as spreadsheets,
today I am doing accounts so Im excell in my head
the data could be sent to mail(), to the user and the site owner, or all of the above.

Hi almostbob

Thanks for that, but to be very honest, the information has flown over my head, could I have it in layman's english, sorry but, thick as thr brown stuff this end.

Member Avatar for diafol

@ AB:

$countfile = "/cgi-bin/countforms.bcnt"; /* define the file containing the count */
$outfile = "/cgi-bin/outputforms.csv"; /* define the file containing the form data, in this case a .csv text file containing 1 row of each form submitted */
$form_id = file_get_contents( $file ) + 1; /* read the last form number and increment */

Is the $file variable = $countfile? I couldn't see it referenced previously.

commented: Thanks, again missed the obvious +4

I have used this little routine to create a unique (ascending) sequence number for registrations. These aren't random, they are very predictable. If you are using the number as a unique reference number, then this is probably what you want to do. It uses a text file so you don't need a database. It locks the file because you could have multiple users trying to do this at the same time (so it needs to be done in a serial fashion).

$fc =fopen("reg_ctr.txt","a+") or die("Error!");
	flock($fc,LOCK_EX);			/* lock the file */
	$seq_ctr = fread($fc,10);
	$seq_ctr_next = $seq_ctr;
 	@$seq_ctr_next = $seq_ctr_next+1;
	ftruncate($fc,0);
	$ret=fwrite($fc,$seq_ctr_next);
	fflush($fc);
	flock($fc,LOCK_UN);                               /* unlock the file */
	$ret=fclose($fc);

In other cases where "semi-unique" is close enough (the number generated is never used on its own to find the associated information), I have used the time stamp as the reference number. If you use the full ten characters, then it is unique unless two threads request a number in the same second. If you limit it to the last six characters, then it is less unique and you will eventually get some duplicates. If you want it more unique, you use microtime and a much longer number. For this type of use, the time-stamp info can also be combined with some other info (e.g. last name or part of the phone number) to make it even more unique.

@ AB:
Is the $file variable = $countfile? I couldn't see it referenced previously.

Thanks, Ardav, the obvious error, wasnt obvious when I looked at it first time, i searched for bugs, and missed it

$form_id = file_get_contents( $countfile ) + 1;

the editor I am trying this time tries to complete the code, great when you can't remember the parameterss, lousy when you walk away to grab a coffee and havent completed the line

Hi,
I also faces this same problem then i found solution which i posted here...

$i=0;
$rand_no="";
while($i<6)
{
	$rand_no=$rand_no.rand(0,9);
       $i++;
}
echo "6 digit random no.".$rand_no;

I hope this code may help you
Thanks 
Tulsa

Hi,
I also faces this same problem then i found solution which i posted here...

$i=0;
$rand_no="";
while($i<6)
{
	$rand_no=$rand_no.rand(0,9);
       $i++;
}
echo "6 digit random no.".$rand_no;

I hope this code may help you
Thanks 
Tulsa

Or, a shorter version of what you've got is this:

$rand_no = rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9);
echo $rand_no
Member Avatar for diafol

It's a shame php doesn't have a python-like zfill() function, then you'd just need the one rand() or mt_rand() call, not 6. Perhaps I'm wrong, anyone out there know of a function? You could always write your own function, like this:

function random_zfill() {
    $int = mt_rand(0,999999);
    for($i=0; $i<(6-strlen($int)); $i++)$zeroes .= '0';
    return $zeroes.$int;
}

$my_number = random_zfill();

Anyway, this is all academic since the thread starter will be using a file-based counter (I assume).
Obviously, you could expand the function to have parameters like start, end etc.

Hi there Ardav

To answer your question, I will be using the code you wrote at the top of this post, it works well and now that I have had to add another digit, I think it will be ok, any case the client is happy, and at the end of the day, that is the main thing. I need to find something to help me write PHP - A job for Christmas and the New Year. THANKS to everyone who has answered my question, and I mean that.

Kind regards John

You could always write your own function, like this:

function random_zfill() {
    $int = mt_rand(0,999999);
    for($i=0; $i<(6-strlen($int)); $i++)$zeroes .= '0';
    return $zeroes.$int;
}

$my_number = random_zfill();

Then why not just this:

function random_zfil() {
return sprintf("%06d",mt_rand(0,999999));
}
Member Avatar for diafol

why not indeed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.