tags:

views:

364

answers:

8

Let's say I'm building a web application whose user pages can be found at http://example.com/NAME. What's the best way to make sure the username doesn't conflict with a reserved word (e.g. 'about', 'contact', etc.)? I can think of two ways:

  • Maintain a list somewhere in my code. This is great and all, but means I have another piece of code I have to edit if I decide to, say, change the "about" page to "aboutus".
  • Request the URI (e.g. http://example.com/someusername) and check if it exists (doesn't return a 404). This feels kind of like a hack, but on the other hand it does exactly what it's supposed to do. On the other hand, I can't reserve anything without making a page for it.

What would be the best way to go about this? Manual validation of usernames is not an option. Thanks!

EDIT: I forgot to mention, the username has to go at the root, like this:

http://example.com/USERNAME

Not like this:

http://example.com/users/USERNAME

Hence why I'm asking this question. This is for technical reasons, don't ask.

+1  A: 

You only know what are these 'reserved' words. So better maintain a list and validate against it.

Another method will be if you use a CMS, then all these keywods 'about', 'contact' etc. will be there in your database. Validate against it.

NinethSense
+2  A: 

How about changing your routing scheme so that users are at example.com/users/NAME ?

spender
A: 

You could always look and see how stackoverflow.com works.

quamrana
That would be "Using a URI scheme that doesn't match the one the question is about"
David Dorward
It seems I answered before the question was fully asked.
quamrana
+1  A: 

How about just create dummy accounts first with all the reserve words? just list all the possible ones and create them.

if you use

www.example.com/user/name

then there will be no problem but it seems like you'd like the URL to be short.

動靜能量
+5  A: 

I would strongly suggest using a unique path like http://example.com/users/NAME instead. Otherwise, what are you going to do if you want to add a reserved word, but a user has already taken it as their user name? You'll end up with all kinds of potential migration problems down the track.

Alternatively, if you must have something that goes straight off http://example.com/, could you possibly prefix all user names? So that user jerryjvl would translate to link http://example.com/user_jerryjvl?

If there is really no other possible solution, then I'd say either check user names against whatever data source determines what the 'reserved words' are, or make a lookup file / table / structure somewhere that contains all the reserved words.

jerryjvl
+2  A: 

In the interest of completeness, if you can't change the routing. Another possibility is to have your user routes and your non-user routes have a programmatic distinction. For example, if you appended a '_' to the end of each of your user routes, then you can make sure that users are located at: http://example.com/NAME_ and the other route would never end in '_'

Sean
Aha! Good idea, that's exactly what I need. It's a hack, but I have to expect that with the conditions I'm working with. Thanks!
musicfreak
There's an existing convention from Unix, prefix ~.
MSalters
@MSalters: Oh yeah, I've seen that before. I'll use that then.
musicfreak
+1  A: 

Right next to the text box something like: "Please use your personal nickname or you real name. Usernames with common words indicating affiliation with the site administration may be revoked".

User
Ah, yes, that will surely make the users think "I shouldn't use this name" if they want to use a "bad" name. Or, no, it won't.
OregonGhost
A: 

Maintain a list somewhere in my code. This is great and all, but means I have another piece of code I have to edit if I decide to, say, change the "about" page to "aboutus".

Your menus should be stored in an array/list. This way you would have only 1 piece of code to edit, not 2. =]

Then, since all menus are in one array, you can match username with elements in the array. for example

$menu = array('About', 'Contact', 'Home')
if( in_array($username, $menu) ) {
  echo 'invalid username'
}
lyrae