How do I start the auto number value at 100000, then increment by 10 upon each record being added to the database.
Example.
Record 1 = 100000
Record 2 = 100010
Record 3 = 100020
Record 4 = 100030
Record 5 ...
How do I start the auto number value at 100000, then increment by 10 upon each record being added to the database.
Example.
Record 1 = 100000
Record 2 = 100010
Record 3 = 100020
Record 4 = 100030
Record 5 ...
Why do you want to do this?
I am not sure how to fix an increment of 10, but you can start from 10000 and then multiply everything by 10 as needed.
I am using it as a id field for users. Each user will have a 10 digit difference, in order to make it less easy to guess.
How do I multiply everything by 10?
I am using it as a id field for users. Each user will have a 10 digit difference, in order to make it less easy to guess.
What's wrong with guessing user's id numbers? If there is an exploit due to someone guessing some user's id number, then systematically generating id numbers in a predictable fashion is a recipe for disaster.
If you have designed your system such as say, randomly generated session ids are used to authenticate users, with user id numbers used to reference users once they have been authenticated, then you can just start from 1 and increment by 1. It does not matter what user id an attacker guesses because they have to guess the session id, not the user id.
I'm using it for Gift Vouchers!
Is it possible to include the current time and may date in the id, upon the gift voucher being purchased? - Though, I want the ID to only be 6 chars long?
Ah, okay. This makes more sense.
Is it possible to include the current time and may date in the id, upon the gift voucher being purchased? - Though, I want the ID to only be 6 chars long?
It is a little difficult to squeeze so much information into 6 characters and yet make it difficult to predict.
Instead of using auto increment, I suggest just taking a hash of a random number and using the first 6 hex digits. With that many possible gift voucher numbers you can expect to run into collisions after a rough estimate of 4096 gift vouchers or so, thus the gift voucher number field should be declared as UNIQUE, and you should handle collisions by generating a new number before issuing it to the user.
Could you give me some example code?
Is it possible to just do something like this?
<Hour> 10 <Day>
e.g. 13 (1pm) 10 (Text) 11 (11 Day) = 131011
How could I make this more complex?
Here is an example of how to generate such a voucher number:
$voucher_number = substr(md5(uniqid(mt_rand(), true)), 0, 6);
The database handling code depends on your database management system and database API, but in pseudocode:
Do
Generate voucher number
Insert voucher details
While there is an error and the error is a unique constraint violation
Note that will only a million possible voucher numbers, if you attempt to give out more than a million vouchers, the loop will become an infinite loop.
I am using MySQL with PHP 5.
What happens if the random number is called twice?
Is it possible to do the method in my previous post, but somehow make it more complex?
mt_rand() generates a pseudorandom number using the Mersenne Twister algorithm.
uniqid() generates a probably unique id based on the current time, prefixing the result of mt_rand().
md5() takes an MD5 hash of the unique id, return a hex string.
strlen() takes the first 6 hex digits of the hash as the voucher number.
What happens if a random number is generated twice?
Is it possible to just use numbers, without letters?
What happens if a random number is generated twice?
As you can see from the pseudocode, a new number is generated to replace it after such a unique constraint violation is detected. Alternatively, you can select from the table to check, but there is a slight risk of a race condition.
Is it possible to just use numbers, without letters?
Yes (e.g., with [man]hexdec/man ), but then there would be only a million possible voucher numbers, so you would expect to get collisions in voucher number generation after about a thousand vouchers are issued. Although the algorithm handles such duplicates, after awhile this may result in a slowdown of the script as it keeps getting duplicates.
Can you just give me a step by step guide, on how the above code works? - What does it do, to give you a result?
$voucher_number = substr(md5(uniqid(mt_rand(), true)), 0, 6);
I wrote that in Post #10 of this thread. Read the PHP Manual concerning the individual functions for further explanation.
Oh Yeah! Sorry.
How do I incorporate hexdec inside the code?
How do I incorporate hexdec inside the code?
After using md5(), before using strlen().
$voucher_number = substr(md5(uniqid(mt_rand(), true)), 0, 6);
How do I display the number generated by the above line of code, in upper case?
Use [man]strtoupper/man.
Where in the following code do I put strtoupper()?
substr(md5(uniqid(mt_rand(), true)), 0, 6);
Where in the following code do I put strtoupper()?
That implies that you still do not understand what the code is doing
At this point I advise you to stop being spoonfed the answers. Try and figure it out yourself. Read the PHP manual again on what each function used actually does. Think of where you should put it, then put it there and see if you are right. If all else fails, you could always rely on brute force (insert strtoupper() at various points until it works, and there are two points where it will work, one being more efficient than the other), but it is better to understand what is happening.