Jump to content

Recommended Posts

I'm constructing a registration process that will do the following :

 

Take all the relevant personal details of a user.  Then I need to assign a 5 (or 6) digit code to that user.  This is only displayed instead of their personal details, unless you are a privileged user.

 

This number is not used to log in, or for any other process at all.  It's just a random number which is displayed instead of things like Name, Address, etc.

 

I've got a database with all those fields and added a unique_num as this number.  This number needs to be generated, and preferably semi random (or even fully random), but I can't have duplicates, as the other users need to request to see the details of user number #676548.

 

Any ideas?  I've looked at rand(), and although thats a great step, I have no control if the number exists.  This is a background operation, and I don't want registrants having to refresh the form due to a potential 'User Number Exists' kind of error.

 

I've got no code really, as I want this to plug in to a registration form, and it's not dependent on anything else except when it writes to the DB.

 

Rich ;D

I did think of that, but I thought that having a random number would reduce the chances of 'guessing' - the other thing I should have asked is -

 

If I go the random option, how can I check that a number already exists?

 

If I do auto-increment - can I have 2 auto-increments in a table?  Sorry if that's a little n00b - but I've only ever seen 1 auto inc in tables.

 

Rich

Why would you ever need 2? And security through obscurity is a bad practice, imo. If you don't want a user to view the page, build a user authentication system, and deny all others access.

 

And verifying a random number is unique can be done in 2 ways... You can generate the number and query the DB for it and see if it exists, if it does, generate a new number and repeat. Or you can simply attempt to insert the number to a unique column. If the query returns an error, the id probably already exists in the table... generate a new number, repeat.

Guess that told me!  :D

 

I wasn't trying to do anything by security through obscurity.  I just wanted people not to figure out that the numbers could be sequential.

 

It's not used for authentication in any way, just a 'mask' I guess so instead of showing :

 

Name : My name.

 

Address : My Address.

 

Tel No : My Tel No's.

 

It would just have :

 

Client ID : 235488

 

and a contact this client button.  Which if the client verifies, will unlock it for that other client ID. 

 

Hope this clears things up a little.  Just realised I would have to create a connection table - so each ID can be verified to allow access.

 

Rich (now getting confused)

I like this one a little better myself.

 

$id = substr(md5(uniqid(rand(), true)), 0, 6);

 

Your code will return the same result every second.. so if a duplicate ID is found ,the script could loop for as long as a second, continually polling the DB. The function above should return a new value at the very least every millisecond, assuming rand() returned the same result twice in a row

Your code will return the same result every second.. so if a duplicate ID is found ,the script could loop for as long as a second, continually polling the DB. The function above should return a new value at the very least every millisecond, assuming rand() returned the same result twice in a row

 

You could always use microtime()

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.