How can alphanumeric IDs be generated in PHP or AS3 etc, instead of 开发者_如何学Pythonthe classic auto incremented numeric ID.
YouTube uses this for its video IDs for example. Facebook generates long alphanumeric strings for uploaded image names and so on.
EDIT: It's not how I generate the strings per se, but how do I generate unique ones like the auto_increment column in a MySQL column would.
In PHP you may use base_convert to change to a larger number base (up to 36, the format will be 0-9a-z, with a being equal to 10 and so on).
Regards, Alin
To guarantee uniqueness you need to come up with the ID on the server side. Then it can be for example a MySQL cell that you use to get the next number. If you have a 64bit number, you can also generate the number when the page is loaded regardless of whether in the end a unique ID is needed or not, if you need to have it always embedded in the JavaScript file. Then if the user for example saves the data, you can store the ID submitted along with it. You don't need to keep the unused ID numbers as long as you make sure the counter is reliably incrementing, so using MySQL for this you just need one cell to always increment in addition to the actual data you're saving.
With 64 bits you'll never run out of IDs and you can reorder the bits if you want it to be less predictable what the next ID will be. If any kind of security is needed, you should look more into a 1:1 mapping from the running numbers into less predictable ones.
As for where to keep the incrementing number it can be a row in any SQL database, or a simple text file if you handle locking, or if you can have a program always running on the server you can communicate with it using any kind of IPC to get new numbers. Just make sure if the server reboots the sequence will continue at least from where it was left.
Best way for uniqueness is to combine the current time/date in milliseconds with a random number.
Take a look at UUID
's
http://en.wikipedia.org/wiki/Universally_unique_identifier
they don't look like the strings at youtube, but they are defiantly unique
AS3
import mx.utils.UIDUtil;
var uuid:String = UIDUtil.createUID()
PHP
$unique = md5( uniqid() ); // 32 characters long
$unique = sha1( uniqid() ); // 40 characters long
精彩评论