Forum Moderators: coopster

Message Too Old, No Replies

user id column, auto increment

         

Tommybs

11:35 am on Jan 9, 2010 (gmt 0)

10+ Year Member



Hi,

Often when I create a user table I create a user_id column as the Primary Key and Auto Increment. Then when I create a member page I normally relate the user to this id.

e.g member.php?id=2 would be the 2nd user entered in the database. Just recently I've been thinking that this might not be the best way to go about it as anyone would know the exact row on the table a user sits on. Am I better not exposing this PK but instead creating another field e.g uid which is in face some kind of hash of the time they regsitered or something unique number? e.g. uniqid(rand()); kind of style?

I notice a lot of the big boys must seem to do something similar as they don't seem to have these incrementing numbers.

Many Thanks

mack

1:03 pm on Jan 9, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



You could use the server timestamp as a user identifier, but still use the auto inc id for back end stiff. Either way the info that is displayed will in some way relate to the db.

Mack.

Tommybs

1:35 pm on Jan 9, 2010 (gmt 0)

10+ Year Member



yeah I'm aware it will in someway relate to the DB, just didn't know if there were some wider security implications to not exposing the PK. As you say it might be better to use the PK for back end stuff, updates and inserts and the other hash as a read only identifier.

FourDegreez

2:37 pm on Jan 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not aware of any security reason for hiding this piece of data, unless any of your scripts blindly trusts it to be the ID of the logged in user. If that's the case, a malicious person could easily guess valid IDs of other users and do harm.

But there are some good reasons not to expose this value. For sites just starting out, you may not want to expose the fact that your user base is small. For many, it's common practice to initialize the starting value at some larger number, say 10000. Then again, for some users it is a point of pride to have a low user ID.

Another reason not to expose the number is for SEO or just having nicer URLs. Instead of /profile.php?memberId=123 you could have /profile/username

What I do is, maintain a separate field in addition to username that is set to a lowercase version of username with spaces stripped out. So if the username is Cool Guy, this other field will be coolguy. And I'd use that as the visible key in a URL like /profile/coolguy