What is the fastest way to generate a unique id number when registering a user

I have a client who wants to register users and give them a unique ID number when they complete registration. (Kind of a member number sort of thing)

Can I just setup the WP member table to start id numbers in the high 10 digit range?

Read More

User experience would be:

  1. user would visit the site
  2. user would register
  3. WP would issue an 10 digit id number

Related posts

Leave a Reply

3 comments

  1. The ID column of the users table is AUTO_INCREMENT, so you can set the base counter to a high number:

    ALTER TABLE wp_users AUTO_INCREMENT = 1000000000;
    

    All new users will have an ID that is higher than this value. Calling this operation is safe, if you accidentally set it to a low number either nothing happens (in a InnoDB table), or it is set to the highest existing number + 1 (in a MyISAM table).

  2. The ID field will definitely work. If you wanted a larger number you could just add 10,000 to it and it will still be unique.

    Other ways of doing this get more involved. You can store a value in the wp_options table, and then fetch, increment, save, but you have a small window of opportunity where two people could end up with the same number, so you need to check for that and repeat if there was a collision. You can generate MD5 (or SHA1) hashes from some chosen bits of user info, but this will give you a big, nasty string of characters. This can be useful when generating a UUID (Universally Unique ID) across multiple systems because the chance of collision is vanishingly small.

  3. I guess also easy would be to hook into the registration process (pre data get’s submitted to DB) and just change the user_registered column from wp_usermeta table with some preg_replace for empty spaces. They would be unique as long as you’re not submitting them in the exact same second. The value won’t ever be changed by anyhting after the registration process + it would allow ordering by registration time if you may need that.