If your requirements:
- It should be reversible (that is, given just a "random" identifier, you can find the original order_id)
- No extra columns
- You do not want to show the source / internal order_id to the user at all
then I would recommend some kind of two-way encryption. Hashing will not work as you cannot find the original value from the hash.
I also add that it should be convenient for a person, for example. someone can call you
I am going to use a very simple two-way encryption class located here , which was written by Tony Marston.
We want the solution to be convenient for people, so remove some of the scrambling symbols. I left only uppercase characters, numbers and space and hatching characters. All of them can be easily transmitted using the standard phonetic alphabet, and the forced use of uppercase eliminates any confusion as to what character.
These are the scrambling strings that I used (I used this online scrambler instead of trying to cross the string myself):
$this->scramble1 = '0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ '; $this->scramble2 = 'UKAH652LMOQ FBDIEG03JT17N4C89XPV-WRSYZ';
So the code to create our friendly person order id:
<?php include 'encryption_class.php'; $crypt = new encryption_class(); $key = "A-COMPLETELY-RANDOM-KEY-THAT-I-HAVE-USED"; // Min length of 8 for encrypted string $min_length = 8; $order_id = 123456789; print "Original: " . $order_id . PHP_EOL; $encrypt_result = $crypt->encrypt($key, $order_id, $min_length); print "Encrypted: " . $encrypt_result . PHP_EOL; // DECRYPT $decrypt_result = $crypt->decrypt($key, $encrypt_result); print "Decrypted: " . $decrypt_result . PHP_EOL; ?>
(You need to download and save the * encryption_class * file locally and enable it).
I ran this code from the command line and got the following output:
Original: 123456789 Encrypted: 2UD5UIK9S Decrypted: 123456789
We now have a short, user-friendly order_id that can be used in a URL such as http://myapp.example.com/order/view/2UD5UIK9S , and you never need to display or pass an internal order_id for your users .
Notes:
The encrypted code will be unique if your order_id is unique (since it will be PK)
This should not be used as a password encryption / decryption procedure - do not store passwords, store hashes.
Make sure your secret key is random, complex, and contains only characters in your $ scramble variables.
It only confuses order_id.
Edit:
Although padding the input line (order_id) creates a certain amount of ramdomness, you can combine this with @biakaveron's answer to create a url like http://myapp.example.com/order/view/5cc46aea44e898c3b4e1303eb18d8161302cd367/2UD5UIK9S