1

I want to generate a Unique Code for each project being created. I have an HTML5 webpage that allows user to create new project, each project when created successfully be assigned a unique code.

I am making a Ajax call to the PHP file on the web server which in-turns saves the project details in MySql database. I have a column in the table that stores unique code for each project created.

I am confused how do i create this code ? is it in PHP or shall i do it in MySql. I want it to be a unique code which will be used by the client to distribute to their customers.

I haven't decided on the length of the key yet but it should be around 8 Digits(combination of char & int is fine ). I know i could use HashTable in Java to create this code based on the inputs from user but i am a fresher to PHP/MySql.

Any advise ?

Note: My Aim is that the key should not be repeated

dev_marshell08
  • 1,051
  • 3
  • 18
  • 40
  • In mysql there is an auto-increment. but this is using a numeric only. If you want, you just create a function or just a simple query that concatenate series the date and time it is created. and the auto-increment number (primary key) plus what ever letters on the project. Hope this helps. – noobdeveloper May 07 '14 at 03:46
  • Have a look at this [http://stackoverflow.com/questions/55218/unique-key-generation](http://stackoverflow.com/questions/55218/unique-key-generation).. This may help you to get more dimensions – shatheesh May 07 '14 at 03:53

4 Answers4

2

You can use PHP's uniqid() to generate a unique ID. However, this should not be used for security purposes, as explicity stated in the PHP manual. For more info, go here

Example:

$unique_key = uniqid();

echo $unique_key; // Outputs unique alphanumeric key, like 5369adb278516
Mark Miller
  • 7,442
  • 2
  • 16
  • 22
  • thanks for the response Mark, quick question will this always generate a unique code ? Is there a possibility of it generating similar codes ? – dev_marshell08 May 07 '14 at 04:09
  • Similar - yes. Duplicates - no. I'm not sure exactly how the algorithm works, but it generates based on the current timestamp in microseconds. So yes, you will get some relatively similar codes - like `5369abd278516` and `5369b6a3c7a64`, but they will always be unique - you will never get the same code twice. – Mark Miller May 07 '14 at 04:30
  • @dev_marshell08 I should clarify my last comment - _never_ is a strong word. I suppose it is possible to generate a duplicate if you created 2 codes within the same microsecond - in which case there is probably a ridiculously, astronomically small chance of creating the same key. For all intents and purposes, though, you are safe to assume that it will always be unique. – Mark Miller May 07 '14 at 04:36
0

Generate Code:

// $length is the length of code you want to return
function generate_code($length) {
    $charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789012345678900987654321234567890";
    return substr(str_shuffle($charset), 0, $length);
}

To get the verification code, it will call user_code_exists() with a parameter of the generated code which is on $code = generate_code(50).

It will check the database if there's at least one row that has the same value, if the row is 0 (code doesn't exist) it will return as true.

// Do generate and verify code existence
$verification_code = "";
while($this->user_code_exists($code = generate_code(50)) == true) {
    $verification_code = $code;
    break;
}

public function user_code_exists($code) {
    $query = $this->db->prepare("SELECT verification_code FROM accounts WHERE verification_code = :verification_code");
    $query->execute(array(':verification_code' => $code));
    return ($query->rowCount() == 0) ? true : false;
}

On while loop, once it returns true, the variable $verification_code holds the unique generated code.

This is just an overview, I hope this helps.

Wesley Brian Lachenal
  • 4,381
  • 9
  • 48
  • 81
0

See the answers given for this question:

What is the best way to create a random hash/string?

In particular, if you want a purely random value (as opposed to, say a hash of the project name) then see the answer by @Gajus Kuizinas, except using base64_encode rather than binhex will give a shorter but still readable value:

base64_encode(mcrypt_create_iv(8, MCRYPT_DEV_URANDOM));

will give you 11 characters: NTM2OWI0YzR

Or if you don't have the mcrypt library installed, try:

base64_encode(hex2bin(uniqid()."0")); // Derived from microtime (the "0" is needed since uniqid() gives an odd number of characters

gives 10 characters: U2m5vF8FAA after discarding the trailing '=='

If you want to be paranoid about the project code never repeating, add a unique index to the column in your MySql table that stores the unique code for each project created, and repeat the number generation if your insert into the table fails.

As noted by @Mark M above, if you are concerned about security or someone masquerading an existing project code, see @Anthony Forloney's answer in the related question link above. In particular:

Numbers used once (NONCE) - They are used on requests to prevent unauthorized access, they send a secret key and check the key each time your code is used.

You can check out more at PHP NONCE Library from FullThrottle Development

Community
  • 1
  • 1
Mike
  • 3,722
  • 1
  • 28
  • 41
0

I needed to do something similar, a solution to keep unique id and i ended up with a solution to use PHP function time() like this $reference_number = 'BFF-' . time(); you can change the BFF to something that makes more sense to your business logic. This way i dont have to worry about if new id that is being generated was taken up before.

I hope this helps

Shairyar
  • 3,268
  • 7
  • 46
  • 86